通過命令行給APP推送消息,有幾個用處,一個是調(diào)試,不依賴服務(wù)器端配合,自己可以構(gòu)造各種各用的內(nèi)容,比如測試提示音什么的;另外就是驗證推送證書是不是好用的。證書過期更新之后,自己驗證一下沒問題再發(fā)給服務(wù)器端的同學(xué)比較好。
下面就簡單介紹一下這個工具。Nomad包含一系列命令行工具。其中Houston是做推送的。安裝命令如下。
$ sudo gem install houston --verbose
只需要拿到DeviceToken和推送證書就可以輕松推消息了。DeviceToken需要調(diào)試APP、打日志、找后臺同學(xué)要等途徑拿到。因為服務(wù)器端用的是推送證書是p12格式的,需要轉(zhuǎn)換成pem格式。轉(zhuǎn)換格式如下所示。
$ openssl pkcs12 -in laiwang_apn_rc.p12 -out laiwang_push_notification.pem -nodes -clcerts
準(zhǔn)備好之后,就可以推消息啦。這里需要注意的是環(huán)境設(shè)置,sandbox和production別搞錯了。不出意外的話,很快就能收到提示。
$ apn push "<7c4f1b12 41fbfa59 cf3a0fc6 10d88871 2a0ef4a4 be2ed8a2 46bef6f4 052c1fbe>" -e production -c laiwang_push_notification.pem -m '我愛你呀'
1 push notification sent successfully
Houston主頁給了一個腳本,可以定制很多內(nèi)容。比如設(shè)置badge、提示音,填寫custom_data等等。
require 'houston'
# Environment variables are automatically read, or can be overridden by any specified options. You can also
# conveniently use `Houston::Client.development` or `Houston::Client.production`.
APN = Houston::Client.development
APN.certificate = File.read("./laiwang_push_notification.pem")
# An example of the token sent back when a device registers for notifications
token = "<7c4f1b12 41fbfa59 cf3a0fc6 10d88871 2a0ef4a4 be2ed8a2 46bef6f4 052c1fbe>"
# Create a notification that alerts a message to the user, plays a sound, and 0fc6 10d88871 2a0ef4a4 be2ed8a2 46bef6f4 052c1fbe>sets the badge on the app
notification = Houston::Notification.new(device: token)
notification.alert = "Hello, World!"
# Notifications can also change the badge count, have a custom sound, have a category identifier, indicate available Newsstand content, or pass along arbitrary data.
notification.badge = 57
notification.sound = "default"
notification.category = "INVITE_CATEGORY"
notification.content_available = true
notification.custom_data = {foo: "bar"}
# And... sent! That's all it takes.
APN.push(notification)