用 Houston 在本地调试远程通知

来源:互联网 发布:用手机淘宝怎么挣钱 编辑:程序博客网 时间:2024/05/28 06:07

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证书文件:

  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’
2 0
原创粉丝点击