调试远程推送

 

用 Houston 在本地调试远程通知 

分类: iOS开发 分享 | 随笔   1387人阅读  评论(1)  收藏  举报

目录(?)[+]

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

  1. Keychain Access里选择你的Push Services证书和通过它左侧的小箭头展开的私钥文件
  2. 选择这两个文件后,右键选择Export 2 items...
  3. 选择p12格式并将导出的文件命名为cert.p12
  4. 最后将这个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命令里指定DeviceTokenpem证书路径。

普通文本

比如测试一下普通的文本通知:

<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’
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值