其它技术点仓前iOS研究组

使用Houston给iOS APP推送信息

2016-11-30  本文已影响60人  阿呆少爷

通过命令行给APP推送消息,有几个用处,一个是调试,不依赖服务器端配合,自己可以构造各种各用的内容,比如测试提示音什么的;另外就是验证推送证书是不是好用的。证书过期更新之后,自己验证一下没问题再发给服务器端的同学比较好。

下面就简单介绍一下这个工具。Nomad包含一系列命令行工具。其中Houston是做推送的。安装命令如下。

$ sudo gem install houston --verbose

只需要拿到DeviceToken和推送证书就可以轻松推消息了。DeviceToken需要调试APP、打日志、找后台同学要等途径拿到。因为服务器端用的是推送证书是p12格式的,需要转换成pem格式。转换格式如下所示。

$ openssl pkcs12 -in laiwang_apn_rc.p12 -out laiwang_push_notification.pem -nodes -clcerts

准备好之后,就可以推消息啦。这里需要注意的是环境设置,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主页给了一个脚本,可以定制很多内容。比如设置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)
上一篇下一篇

猜你喜欢

热点阅读