1. 程式人生 > >用 Houston 在本地除錯遠端通知

用 Houston 在本地除錯遠端通知

Houston 的背景

Houston 在 GitHub 上的地址:https://github.com/nomad/Houston,作者又是Mattt Thompson,簡直是慘無人道啊,又高產,又有質量這裡寫圖片描述

Houston 能讓我們在本地、甚至終端很方便的除錯遠端通知。

安裝

首先在終端安裝:

$ gem install houston

我第一次安裝的時候報了錯,但是第二次就安裝成功了:

...
Installing ri documentation for houston-2.2.3
1 gem installed

安裝成功後執行gem list命令可以看到 houston 和它的版本號:
這裡寫圖片描述

然後也能執行apn命令了:

$ apn
error: undefined method `size' for nil:NilClass. Use --trace to view backtrace

準備證書

在正式的使用之前需要先準備好pem證書檔案:

  1. Keychain Access裡選擇你的Push Services證書和通過它左側的小箭頭展開的私鑰檔案
  2. 選擇這兩個檔案後,右鍵選擇Export 2 items...
  3. 選擇p12格式並將匯出的檔案命名為cert.p12
  4. 最後將這個p12檔案轉成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命令裡指定DeviceTokenpem證書路徑。

普通文字

比如測試一下普通的文字通知:

$ 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’