用 Houston 在本地調試遠程通知,houston調試通知
Houston 的背景
Houston 在 GitHub 上的地址:https://github.com/nomad/Houston,作者又是Mattt Thompson,簡直是慘無人道啊,又高產,又有品質
Houston 能讓我們在本地、甚至終端很方便的調試遠程通知。
安裝
首先在終端安裝:
$ gem install houston
我第一次安裝的時候報了錯,但是第二次就安裝成功了:
...Installing ri documentation for houston-2.2.31 gem installed
安裝成功後執行gem list命令可以看到 houston 和它的版本號碼:
然後也能執行apn命令了:
$ apnerror: undefined method `size' for nil:NilClass. Use --trace to view backtrace
準備認證
在正式的使用之前需要先準備好pem認證檔案:
$ openssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts
如果你的 Keychain Access 裡沒有 Push Services 認證的話,你可能需要下載或建立一個:
1. 登入到 iPhone Developer Connection Portal(http://developer.apple.com/iphone/manage/overview/index.action )並點擊 App IDs
2. 建立一個不使用萬用字元的 App ID ,這是因為萬用字元 ID 不能用於推播通知服務
3. 點擊 App ID 旁的“Configure”,然後按下按鈕生產 推播通知許可證。根據“嚮導” 的步驟產生一個簽名並上傳,最後下載產生的許可證
4. 通過雙擊 .cer 檔案將你的 aps_developer_identity.cer 添加到 Keychain Access中
5. 添加後Push Services認證後就開始pem檔案,步驟同上
然後就能在終端測試遠程通知了。
發送遠程通知
需要在apn命令裡指定DeviceToken和pem憑證路徑。
普通文本
比如測試一下普通的文本通知:
$ apn push "<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>" -c ~/Desktop/apple_push_notification.pem -m "Hello"
成功後的終端提示:
1 push notification sent successfully
自訂參數
帶參數的遠程通知:
$ apn push "<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>" -c ~/Desktop/apple_push_notification.pem -m "Hello" -d content-id=42
自訂多個參數
多個參數之間用逗號分隔:
$ apn push "<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>" -c ~/Desktop/apple_push_notification.pem -m "Hello" -d content-id=42,icon=image.png
靜默通知
$ apn push "<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>" -c ~/Desktop/apple_push_notification.pem -s "" -n
靜默通知要把 sound 設為空白字串,並且要啟用content_available。
終端可用選項
- ‘-m’, ‘–alert ALERT’, ‘Body of the alert to send in the push notification’
- ‘-b’, ‘–badge NUMBER’, ‘Badge number to set with the push notification’
- ‘-s’, ‘–sound SOUND’, ‘Sound to play with the notification’
- ‘-y’, ‘–category CATEGORY’, ‘Category of notification’
- ‘-n’, ‘–[no]-newsstand’, ‘Indicates content available for Newsstand’
- ‘-d’, ‘–data KEY=VALUE’, Array, ‘Passes custom data to payload (as comma-delimited “key=value” declarations)’
- ‘-P’, ‘–payload PAYLOAD’, ‘JSON payload for notifications’
- ‘-e’, ‘–environment ENV’, [:production, :development], ‘Environment to send push notification (production or development (default))’
- ‘-c’, ‘–certificate CERTIFICATE’, ‘Path to certificate (.pem) file’
- ‘-p’, ‘–[no]-passphrase’, ‘Prompt for a certificate passphrase’