Houston 的背景
Houston
在 GitHub 上的地址:https://github.com/nomad/Houston,作者又是Mattt Thompson
,简直是惨无人道啊,又高产,又有质量
Houston 能让我们在本地、甚至终端很方便的调试远程通知。
安装
首先在终端安装:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ gem install houston</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
我第一次安装的时候报了错,但是第二次就安装成功了:
<code class="language-baseh hljs r has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>
Installing ri documentation <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> houston-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.2</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.3</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> gem installed</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
安装成功后执行gem list
命令可以看到 houston 和它的版本号:
然后也能执行apn
命令了:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ apn
error: undefined method `size<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">' for nil:NilClass. Use --trace to view backtrace</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
准备证书
在正式的使用之前需要先准备好pem
证书文件:
- 在
Keychain Access
里选择你的Push Services
证书和通过它左侧的小箭头展开的私钥文件 - 选择这两个文件后,右键选择
Export 2 items...
- 选择
p12
格式并将导出的文件命名为cert.p12
- 最后将这个
p12
文件转成pem
文件
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ openssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
如果你的 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
证书路径。
普通文本
比如测试一下普通的文本通知:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ apn push <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>"</span> -c ~/Desktop/apple_push_notification.pem -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
成功后的终端提示:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> push notification sent successfully</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
自定义参数
带参数的远程通知:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ apn push <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>"</span> -c ~/Desktop/apple_push_notification.pem -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello"</span> <span class="hljs-operator" style="box-sizing: border-box;">-d</span> content-id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">42</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
自定义多个参数
多个参数之间用逗号分隔:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ apn push <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>"</span> -c ~/Desktop/apple_push_notification.pem -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello"</span> <span class="hljs-operator" style="box-sizing: border-box;">-d</span> content-id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">42</span>,icon=image.png</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
静默通知
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ apn push <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<d10c67b0 079cfdc0 27b5dd81 2fd537ce 16831f40 ca55fa34 8b14ffde 626435f6>"</span> -c ~/Desktop/apple_push_notification.pem <span class="hljs-operator" style="box-sizing: border-box;">-s</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span> -n</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
静默通知要把 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’