APPLE推送通知APNS

googleAPNS:https://code.google.com/p/apns-php/wiki/CertificateCreation

推送证书创建:

1,登陆官网:iPhone Developer Program Portal

2,选择APP IDs。 (or click here)

3,创建一个app id,注意不要选择Explicit,不要选择Wildcard。

4,继续向下进行。直至完成创建。--->创建PUSH SSL CER(Apple Documentation: Creating the SSL Certificate and Keys)

5,生成SSL证书后,双击加入Keychain中(创建成功的话,证书下方会有私钥)。

6,在keychain中,右击证书,导出证书为**.p12(不要注入密码)。

7,开启终端,更改目录到p12文件所在处,用命令将PKCS12证书更改为PEM格式文件,命令:

    <span class="pln">openssl pkcs12 </span><span class="pun" style="color:#66660;">-</span><span class="kwd" style="color:#0088;">in</span><span class="pln"> **</span><span class="pun" style="color:#66660;">.</span><span class="pln">p12 </span><span class="pun" style="color:#66660;">-</span><span class="kwd" style="color:#0088;">out</span><span class="pln"> **</span><span class="pun" style="color:#66660;">.</span><span class="pln">pem </span><span class="pun" style="color:#66660;">-</span><span class="pln">nodes </span><span class="pun" style="color:#66660;">-</span><span class="pln">clcerts</span>

8,在后台ApnsPHP中应用PEM文件。


APP中APNS的实现:ObjectiveC page

推送通知至某设备,DeviceToken是依据DeviceID和APPlicationID,经由apple生成。故是唯一的(per device and per application)。

code demo: http://code.google.com/p/apns-php/source/browse/trunk/Objective-C%20Demo

----------------------------------------------------------------------------------------------------

简述:

想让应用程序的推送功能正常工作,你需要相当多的努力,这个过程非常繁琐。以下是这个过程的概述图:

1.应用程序需要激活推送通知功能。在使用之前,用户需要确认他是否愿意接受这些推送功能。

2.应用程序接到一个“device token”。你可以认为“device token”是推送通知发送信息的目的地址。

3.应用程序会将“device token”发送给你的服务器

4.当你的应用程序发生了有趣的事情,你的服务器向 “苹果推送通知的服务器(Apple Push Notification Service,缩写,APSN)”发送通知。

5.APSN再向用户的设备(例如,iPhone,iPad等)发送通知。

当用户的设备接受到推送通知,将会显示提醒框,播放提示声音,更新图标的未读信息数目。用户可以通过点击提醒框来加载应用程序,苹果公司给推送通知加入了可选内容,你可以根据需求来操作推送通知。

当iOS4实现了本地通知和多任务,苹果的推送通知是否还有使用价值?当然有!

本地通知被定时事件调度限制,并且只有VOIP、导航、后台音频播放这些应用在后台没有被限制。如果当应用程序处于关闭状态时,你想将外部事件的发生通知给你的应用程序用户,仍然需要推送通知。

在这篇教程中,我将解释推送通知系统实现的细节以及如何在应用中建立推送。这里有很多要解释的,这需要(您)花点儿时间去明白。

你需要为推送做些什么

在你的应用中添加推送通知,你需要:

一台iPhone或者iPad 推送通知不能在模拟器上实现,所以你需要在设备上测试。

一个iOS开发者证书 你需要一个新的AppID和每个应用程序使用的推送证书,推送服务器需要的“SSL ”证书,你可在iOS开发网站上做这些。

如果你想跟随这篇教程的例子学习,你将需要创建你自己的推送证书和SSL证书,你不可以使用我的这些证书。因为获得正确的证书是很重要的,我将详细解释如何得到一个推送证书。

一个联网的服务器 推送通知通常是由服务器来发送的。对于开发,你可以使用一个Mac来代替服务器(我们将在教程中这样做),但是发布的产品,你至少需要类似VPS( Virtual Private Server )的服务器。

一个廉价的共享虚拟主机账户不能满足发布产品的需要。你需要在服务器的后台启动一个进程,安装一个SSL证书,并且能够在某个端口中能够外联TLS。

大部分的共享虚拟主机并不让你实现这些功能,即使如果申请的这些需求通过了。无论怎么样,我真的建议你使用一个VPS主机,像Linode。

解析一个推送通知

你的服务器负责创建一个推送通知消息,所以了解一个推送通知消息的构成是有必要的。

一个推送通知是一个短信息,由“device token”,“payload–负载内容”,和其他的一些字节组成。“payload–负载内容”是我们感兴趣的部分,因为它包含着我们实际想发送出去的数据。

你的服务器必须提供“payload–负载内容”,它是以JSON的字典的数据格式来组织数据的。下面是一个很简单的推送消息payload:

<ol class="linenums" style="margin:0px 0px 0px 40px; padding:0px 0px 0px 27px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; line-height:1.5em; font-family:inherit; font-weight:inherit; list-style:none"><li value="1" class="L0" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">{</span></li><li class="L1" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L2" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">    </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"aps"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span></li><li class="L3" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L4" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">    </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">{</span></li><li class="L5" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L6" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">        </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"alert"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"Hello, world!"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">,</span></li><li class="L7" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L8" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">        </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"sound"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"default"</span></li><li class="L9" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L0" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">    </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">}</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L1" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L2" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">}</span></li></ol>

对于不了解JSON的人,一个block的划分由一对花括号“{}”包裹,其中包含一个由“键/值 (key/value)”对组成的“字典”,(就像NSDictionary)。

“payload–负载内容”就是一个“字典”,包含了至少一个“aps”项,“aps”本身也是一个“字典”。在我们的例子中,“aps”包含“alert”和“sound”字段。当这个推送通知被接收后,它将显示一个包含“Hello, world!”内容的提醒框,并且播放标准的提示音。

你可以向“aps”添加另外的选项,来配置通知,例如

<ol class="linenums" style="margin:0px 0px 0px 40px; padding:0px 0px 0px 27px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; line-height:1.5em; font-family:inherit; font-weight:inherit; list-style:none"><li value="1" class="L0" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">{</span></li><li class="L1" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L2" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">    </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"aps"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span></li><li class="L3" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L4" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">    </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">{</span></li><li class="L5" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L6" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">        </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"alert"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span></li><li class="L7" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L8" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">        </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">{</span></li><li class="L9" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L0" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">            </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"action-loc-key"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"Open"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">,</span></li><li class="L1" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L2" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">            </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"body"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"Hello, world!"</span></li><li class="L3" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L4" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">        </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">},</span></li><li class="L5" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L6" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">        </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"badge"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="lit" style="font-family:inherit;color:#06666;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">2</span></li><li class="L7" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L8" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">    </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">}</span></li><li class="L9" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L0" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">}</span></li></ol>

现在“alert”是一个字典。”action-loc-key” 对应的value替代了“View”按钮上的文本内容,”badge”字段包含的数字将被显示在应用图标上,这个通知不会播放提示音。

有很多途径去设置JSON的“payload–负载”内容,你可以改变播放声音,你可以提供本地化的文本,并且添加自己的字段。更多信息,请详见官方Local and Push Notification Programming Guide。

推送通知的目标就是精短;“payload–负载内容”的大小不能超过256个字节。这样留给你的空间和一条短消息或者一个tweet消息的大小一样。一个小型的推送服务不会在换行符和空格上浪费空间,这样一条推送就像下面所显示的:

{“aps”:{“alert”:”Hello, world!”,”sound”:”default”}}

上面这条消息可读性比较低,但是它节省了足够的字节,所以牺牲可读性是值得的。如果一个推送通知的payload超过了256个字节,那么这个推送就不会被“APNS”接受。

关于推送通知常见的错误

推送通知是不可靠的

推送通知是不可靠的!即使APNS(Apple Push Notification Service苹果推送通知服务)服务器接受了推送通知,仍然无法保证该通知最终会被送达。

就你的服务器而言,推送通知会被发出并且遗忘掉;当你将通知发送到APNS后,没有办法查出它所处的状态。通知送达的时间也从几秒到半小时不等。

同样,用户的iPhone不是所有时间都可以收到推送通知。比如,因为指定的端口被封,手机处于一个不允许和APNS连接的WIFI网络。或者手机已经关机了。

APNS将会在手机连接到可用网络后下发从该机器收到的最后一条通知,但是只会尝试有限的时间。一旦发送超时,此条通知就会永远丢失!

After looking at the APNS Server Bill

推送通知会使开销很大!如果你掌控了需要推送的内容,在你的应用中加入推送功能相当容易和廉价,但是当你有好多用户和数据需要轮询的时候,这个功能就会使得服务器开销庞大(译者注:不一定指价格,包括是资源消耗)。

比如,你打算在自己的RSS订阅更新的时候通知用户,这样做没问题。因为你控制着RSS订阅,并且知道它何时发生变化——当你更新网站内容时——于是你的服务器可以在合适的时候发出通知。

但是如果你的应用是一款RSS阅读器,允许用户添加自定义的链接呢?这种情况下,你需要构建一些机制去检测那些订阅的更新。

实际上,这意味着你的服务器为了检查那些订阅的变化,需要不停的轮询它们。如果你拥有很多用户,你可能不得不安装一堆新服务器去处理这些工作和满足带宽需求。对于这类应用,推送会变得异常昂贵,可能不值得去做。

好了,理论上足够了。下面到时间来学习如何使用推送了。在我们投入到美好的事物——编程!之前——有一些无聊的搭建环境的工作需要在iOS Provisioning Portal上完成,所以让我们尽快完成它。

Provisioning Profiles和证书,天哪!

APNS需要一个证书

在你的应用中使用推送通知,需要用一个配置过推送功能的provisioning profile来签名。此外,你服务器所有与APNS的通讯都需要进行SSL证书签名。

Provisioning profile和SSL证书紧密联系在一起,并且只对一个App ID有效。这个保护措施可以保证只有你的服务器可以发推送通知到你的应用,其它服务器不可以。

正如你所知道的,应用程序在开发和发布阶段使用不同的provisioning profiles。同样,推送服务器的证书也有两种:

· Development. 如果你的应用程序运行在debug模式,并且使用的是Development provisioning profile (Code Signing Identity 是 “iPhone Developer”)签名的,你的服务器必须使用Development证书。

· Production. 使用Ad Hoc方式发布的,或发布在App Store(Code Signing Identify 是 “iPhone Distribution”)上的应用程序,必须和使用Production证书签名的服务器通讯。如果这里面有不匹配,推送通知将无法送达你的应用。

在这篇教程里,我们不需要为分发profiles和证书烦恼,只需要使用Development版本的即可。

1,生成证书签名请求(Certificate Signing Request, CSR)

还记得你在注册成为iOS开发者之后,如何去iOS Provisioning Portal生成一个开发证书吗?如果记得,下面的步骤应该会比较熟悉。不过,我仍然建议你准确地按照步骤来做。因为大多数在实现推送通知过程中遇到的问题,都是由于证书问题引起的。

数字证书基于公钥-私钥加密方法。你不需要知道任何关于证书的加密方法,但是你要知道证书一直会与一个私钥搭配使用。

证书是密钥对的非秘密的部分。将它发送给其它人是安全的,比如通过SSL通讯的过程中就会包含证书。然而,对于私钥,当然是私有的。它是秘密的。你的私钥只对你有用,对其他人没用。要重视的是:如果你没有私钥的话,就无法使用证书。

每当申请一个数字证书的时候,你需要提供一个证书签名请求,简称CSR。当你创建了CSR后,会生成一个新的私钥保存到keychain应用程序中。然后你将CSR发送到一个证书颁发机构(目前情况下就是iOS Developer Portal),它会根据CSR中的信息生成SSL证书。

打开Mac中的Keychain Access程序(在Applications/Utilities下),选择菜单中的Request a Certificate from a Certificate Authority…

如果菜单中没有“Request a Certificate from a Certificate Authority with key”选项,就先去下载安装WWDR Intermediate Certificate。并且确认Keychain Access窗口里没有私钥被选中。

现在,你应该会看到下面的窗口:

在里面输入你的邮件地址,听有人推荐说最好使用和注册IOS开发者证号同样的邮件地址,但看起来任何邮件地址都可以。在Common Name中输入“PushChat”。你可以输入任何字符串,但最好是有意义的。这会使你以后容易查找这个私钥。

选择Saved to disk选项,点击Continue。将文件保存为“PushChat.certSigningRequest”。

2,创建 App ID

每个使用 APNs 的 iOS 应用必须有一个唯一的 App ID。在本步骤中,你将学到如何创建推送通知中要用到的App ID。

1. 登录iPhoneDeveloper Program:http://developer.apple.com/iphone/。点击页面右边的“ iPhone Developer Program Portal ”(图4):

APPLE推送通知APNS_第1张图片


2. 首先看到的是欢迎页面(图5):

APPLE推送通知APNS_第2张图片


3. 点击左边的“App ID”,然后点击右边的“New App ID”按钮(图6):

APPLE推送通知APNS_第3张图片


4. 在 Description 栏输入“PushAppID”,在“Bundle Seed ID”栏中选择“Generate New”。在“Bundle Identifier”栏,输入“net.learn2develop.MyPushApp”,然后点击“Submit”(图7):

APPLE推送通知APNS_第4张图片


5. 现在你应该能看到所创建的 App ID 了(图8):



配置 App

一旦创建了 App ID,你还要为推送通知对 App ID 进行一些配置。

1. 点击App ID 右边的 Configure 链接,会看到如下选项(图9):

APPLE推送通知APNS_第5张图片


勾选“Enable for Apple Push Notificationservice”,点击“Development Push SSL Certificate”右边的“Configure”按钮。

2. 接下来你会看到“Apple Push Notification service SSL Certificate Assistant”页面。点击Continue(图10):

APPLE推送通知APNS_第6张图片


3. 点击Choose File 按钮,选择前面保存的证书请求文件存放地址。点击 Generate(图11):

APPLE推送通知APNS_第7张图片


4. 你的SSL 证书会被生成。点击 Continue(图12):

APPLE推送通知APNS_第8张图片


5. 点击Download Now 按钮,下载 SSL 证书。点击 Done(图13):

APPLE推送通知APNS_第9张图片


6. 下载的 SSL 证书文件名为 aps.developer.identity.cer。双击,将证书安装到钥匙串中(图14)。这个证书会在你的程序中用到,它允许程序接收 APNs 发送来的推送通知。

APPLE推送通知APNS_第10张图片


3,创建 Provisioning Profile

接下来,需要创建 provisioning profile 以便允许应用程序安装到真实设备上。

1. 回到iPhone Development Program Portal,点击 Provisioning 栏,点击 New Profile 按钮(图15):

APPLE推送通知APNS_第11张图片


2. Profile Name 栏输入 MyDevicesProfile,在 App ID 栏选择 PushAppID。在Devices 栏,勾选所有你想激活的设备(在 iPhone Developer Program Portal 的 Devices 页中注册的所有设备)。点击 Submit(图16)。

APPLE推送通知APNS_第12张图片


3. provisioning  profile 会等待审核。几秒钟后,它会显示在页面上。点击Download 按钮下载该 provisioning profile(图17):

APPLE推送通知APNS_第13张图片


4. 下载下来的provisioning profile 名为 MydevicesProfile.mobileprovision。

4,激活设备:创建 provision profile 后,你可以将它安装到真实设备中。

--------------------------------------------------

你的app可以查看哪些推送类型被启用,用以下代码来查看

UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

为了能收到推送消息,我们还要在app中添加一些内容。将下列代码添加到PushChatAppDelegate.m中:

<ol class="linenums" style="margin:0px 0px 0px 40px; padding:0px 0px 0px 27px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; line-height:1.5em; font-family:inherit; font-weight:inherit; list-style:none"><li value="1" class="L0" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">-</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">(</span><span class="kwd" style="font-family:inherit;color:#0088;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">void</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">)</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">application</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:(</span><span class="typ" style="font-family:inherit;color:#66066;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">UIApplication</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">*)</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">application didRegisterForRemoteNotificationsWithDeviceToken</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:(</span><span class="typ" style="font-family:inherit;color:#66066;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">NSData</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">*)</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">deviceToken</span></li><li class="L1" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">{</span></li><li class="L2" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">     </span><span class="typ" style="font-family:inherit;color:#66066;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">NSLog</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">(@”</span><span class="typ" style="font-family:inherit;color:#66066;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">My</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> token </span><span class="kwd" style="font-family:inherit;color:#0088;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">is</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">%@”,</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> deviceToken</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">);</span></li><li class="L3" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">}</span></li><li class="L4" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">-</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">(</span><span class="kwd" style="font-family:inherit;color:#0088;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">void</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">)</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">application</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:(</span><span class="typ" style="font-family:inherit;color:#66066;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">UIApplication</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">*)</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">application didFailToRegisterForRemoteNotificationsWithError</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">:(</span><span class="typ" style="font-family:inherit;color:#66066;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">NSError</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">*)</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">error</span></li><li class="L5" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">{</span></li><li class="L6" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">   </span><span class="typ" style="font-family:inherit;color:#66066;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">NSLog</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">(@</span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">"Failed to get token, error: %@"</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">,</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> error</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">);</span></li><li class="L7" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">}</span></li></ol>

当你的应用程序注册远程通知时,它将会尝试获得一个“设备标记(device token)”。

这是一个32字节数字,标识你的设备的唯一性。 可以把device token理解为推送消息的接收地址。

再次运行程序,你应该能在Xcode的控制台窗口看到下面这个:

My token is:

<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>

这个标识是一个封装的二进制数据结构,装入一个NSData对象里。苹果不希望你看见它的内部信息,就我们而言知道它是32字节长就够了。

正如你上面所看到的,标识也可用64位十六进制字符表示,我们将用这种格式使用设备标识,当然还要去掉分隔符和空格。

如果你在模拟器上运行这个程序,由于你的模拟器不支持推送通知,didFailToRegisterForRemoteNotificationsWithError方法将被调用。

这个应用就是这样。还有一件事要做,之后我们马上就可以看到一些推送通知,立即行动!

发送我们第一个推送通知

我之前已经提到了几次,你需要创建一个服务器,它将推送通知给你的app。

第一次测试程序,我们不会去建立一个服务器。相反,我会给你们一个非常简单的PHP脚本,

,来建立一个连接到APNS并发送一个推送通知到您所指定的设备。你可以直接在mac电脑上运行这个脚本

下载这些SimplePush代码并解压缩,你需要在simplepush.php做些改变。

<ol class="linenums" style="margin:0px 0px 0px 40px; padding:0px 0px 0px 27px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; line-height:1.5em; font-family:inherit; font-weight:inherit; list-style:none"><li value="1" class="L0" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="com" style="font-family:inherit;color:#8800;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">//把你的设备标识写在这里(没有空格):</span></li><li class="L1" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">$deviceToken </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">=</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">'0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78'</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">;</span></li><li class="L2" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L3" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="com" style="font-family:inherit;color:#8800;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">//密码放在这里</span></li><li class="L4" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">$passphrase </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">=</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">'pushchat'</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">;</span></li><li class="L5" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span></li><li class="L6" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="com" style="font-family:inherit;color:#8800;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">// 把你的推送消息放在这里:</span></li><li class="L7" style="margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:rgb(238,238,238); font-style:inherit; font-family:inherit; font-weight:inherit; text-align:left; line-height:2em; list-style:decimal"><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">$message </span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">=</span><span class="pln" style="font-family:inherit;color:#000000;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;"> </span><span class="str" style="font-family:inherit;color:#0880;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">'My first push notification!'</span><span class="pun" style="font-family:inherit;color:#66660;margin:0px; padding:0px; border:0px; outline:0px; vertical-align:baseline; background-color:transparent; font-style:inherit; font-weight:inherit;">;</span></li></ol>

你需要从app中拷贝device token到$deviceToken变量。确定删掉了空格以及分隔符。它应该是64位的十六进制字符。把你私钥密码短语放到$passphrase变量,以及你想发送的信息放到$message中。

拷贝你的ck.pem 文件到SimplePush目录,记住,这个ck.pem文件同时包含你的证书和私钥。

然后打开一个终端并键入:

$ php simplepush.php

如果一切顺利,脚本应该会显示:

Connected to APNS

Message successfully delivered

几秒钟内,你应该会收到你的第一个推送通知:

注意, 当应用程序是开着的你不会看到任何东西。消息传过来了,但是我们在app中没有做任何处理消息的方法。

关闭应用程序,然后再试一次。

如果显示一些错误信息,simplepush.php脚本退出,检查你是否正确制作PEM文件,

并且你能正确的连接到沙盒服务器 (见上文)。

现在,脚本究竟做了什么并不重要。在这个系列的第二部分,我们会建立一个真正的推送服务器,到那时候我们会就此做更多的说明。

接下来?

此时,你已经成功建立了一个app来接收推送通知,并且通过自定义的PHP代码发送了第一条推送通知。

----------------------

创建 Push Notification Provider

Push Notification Provider 是一个应用程序,用于通过 APNs 发送推送通知给 iPhone 应用。

通过 APNs 发送推送通知有几个步骤:
1. 使用前面创建的 SSL 证书与 APNs 通讯;

2. 构造所要发送的消息载体;

3. 发送载体到APNs;

APNs 是一个基于流的 TCP socket,你的 provider 以 SSL 协议与其通讯。推送通知(包括载体)是以二进制流的方式发送的。和APNs 建立连接后,你可以维持该连接并在连接中断之前发送多个通知。

技巧: 应避免每发送一次推送通知就建立、关闭一次连接。频繁的建立、关闭连接可能会被 APNs 认为是 DOS 攻击,从而拒绝发送 provider 的推送通知发送请求。

一个推送通知消息的格式如图24 所示:



更多细节,请参考 Apple Push Notification Service Programming Guide

载体(payload)是 JSON 字符串(最长 256 字节),封装了你发送给 iOS 应用的信息。这是一个 payload 的例子:

{

   "aps": {

        "alert" : "Yougot a new message!" ,

        "badge" : 5,

        "sound" : "beep.wav"},

    "acme1" : "bar",

    "acme2" : 42

}


写provider之前,我们需要生成php Push Notification sender需要的证书文件:
  1)在Keychain Access.app里选定这个新证书(Apple Development Push Services*),导出到桌面,保存为Certificates.p12.

APPLE推送通知APNS_第14张图片

APPLE推送通知APNS_第15张图片


  2)然后运行如下命令:

  见第一部分。


下面是一个简单的push notification proivder写法:

 

view plain
  1. <span style="font-size:16px;"><?php  
  2.   
  3. $deviceToken = '38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7ce90d56e9 fe145bcc 6c2c594b'// masked for security reason  
  4. // Passphrase for the private key (ck.pem file)  
  5. // $pass = '';  
  6. // Get the parameters from http get or from command line  
  7. $message = $_GET['message'or $message = $argv[1] or $message = 'Message received from javacom';  
  8. $badge = (int)$_GET['badge'or $badge = (int)$argv[2];  
  9. $sound = $_GET['sound'or $sound = $argv[3];  
  10. // Construct the notification payload  
  11. $body = array();  
  12. $body['aps'] = array('alert' => $message);  
  13. if ($badge)  
  14. $body['aps']['badge'] = $badge;  
  15. if ($sound)  
  16. $body['aps']['sound'] = $sound;  
  17.   
  18.   
  19. $ctx = stream_context_create();  
  20. stream_context_set_option($ctx'ssl''local_cert''ck.pem');  
  21. // assume the private key passphase was removed.  
  22. // stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);  
  23. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195'$err$errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
  24. if (!$fp) {  
  25. print "Failed to connect $err $errstrn";  
  26. return;  
  27. }  
  28. else {  
  29. print "Connection OKn";  
  30. }  
  31. $payload = json_encode($body);  
  32. $msg = chr(0) . pack("n",32) . pack('H*'str_replace(' '''$deviceToken)) . pack("n",strlen($payload)) . $payload;  
  33. print "sending message :" . $payload . "n";  
  34. fwrite($fp$msg);  
  35. fclose($fp);  
  36. ?></span>  


运行结果:Connection OKnsending message :{"aps":{"alert":"Message received from javacom"}}n

效果图在下面。


为了省去自己编写 push notification provider 的麻烦,你也可以使用 Stefan Hafeneger 写的一个 Mac OS X 应用程序:PushMeBaby,下载地址

1. 在Xcode 中打开 PushMeBaby。

2. 右击 Resouces 文件夹,选择 Add Existing Files…,选择前面所下载到的aps.developer.identity.cer 文件(图25)。

APPLE推送通知APNS_第16张图片


3. 在 ApplicationDelegate.m 文件中,修改如下代码(加粗部分):

- (id)init {

   self = [super init];

   if(self != nil) {

       self.deviceToken = @"38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7ce90d56e9 fe145bcc 6c2c594b";

        self.payload = @"{"aps":{"alert":"Yougot a new message!","badge":5,"sound":"beep.wav"},"acme1":"bar","acme2":42}";

       self.certificate = [[NSBundle mainBundle]

           pathForResource:@"aps_developer_identity" ofType:@"cer"];

   }

   return self;

}


4. 按下 ⌘+R,运行程序。将会问你是否允许使用证书,点击Always Allow(总是允许)(图26):

APPLE推送通知APNS_第17张图片


在 iPhone/iPod,确认 ApplePushNotification 程序未运行。点击 Push 按钮,会向设备发送一条推送通知。服务器实际上发送了下列消息给APN 服务器:

{

   "aps": {

        "alert" : "Yougot a new message!" ,

        "badge" : 5,

        "sound" : "beep.wav"},

    "acme1" : "bar",

    "acme2" : 42

}

5. 如果消息推送成功,将会在 iPhone/iPod 上出现下图(图27):

APPLE推送通知APNS_第18张图片


6. 如果现在按下 ⌘+R 调试 ApplePushNotification 程序,然后从 PushMeBaby 中发送一条消息,控制台会显示如下输出:

2009-11-24 21:11:49.182 ApplePushNotification[1461:207]key: acme1, value: bar

2009-11-24 21:11:49.187 ApplePushNotification[1461:207]key: aps, value: {

   alert = "You got a new message!";

   badge = 5;

   sound = "beep.wav";

}

2009-11-24 21:11:49.191 ApplePushNotification[1461:207]key: acme2, value: 42


几个注意的问题:

1.如果申请ssl 证书时不是用的新的apple id,而是原来已经存在的,那么设置好之后要把对应的provisioning profile

也更新一下, 然后去下载新的profile替换掉老的,不然运行会有错。

2.如果你用的是企业版的开发者证书,别人可能没有权限去申请这个ssl 证书,当你替他申请好证书后,应该把证书和证书对

的私钥一起发给他,这样他在本地安装私钥时才会有对应的密钥。

3.当push notification到达时,程序状态不同,效果也是不一样的,一般来说程序可以分为下面三种状态:

1)程序不在运行(后台和前台都不在运行)

这时候如果push notification到了,会弹出一个alertview,当你点击action按钮时,会启动程序,并执行程序delegate.m文件里的

 

 

view plain
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
方法,所以你可以在这里做一些处理:

 

view plain
  1. //看是否有push notification到达,并做相应处理,这个方法和local notification相同,但注意key要对应就行  
  2. UILocalNotification * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];  
  3. if (remoteNotification) {  
  4.     //弹出一个alertview,显示相应信息  
  5.     UIAlertView * al = [[UIAlertView alloc]initWithTitle:@"receive remote notification!" message:@"hello" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];  
  6.     [al show];  
  7.     [al release];  
  8. }  


2)程序在运行(不论是在前台还是后台)

当push notification到达时,如果程序在前台运行并不会弹出alertview,而是直接执行下面方法:

 

view plain
  1.   
  2. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  
  3.       
  4. UIAlertView * al = [[UIAlertView alloc]initWithTitle:@"receive remote notification!" message:@"hey" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];  
  5.     [al show];  
  6.     [al release];  
  7. }  

在这个方法里你可以获取到userInfo字典来进行相应处理。

如果程序是在后台运行,则会弹出一个alertview,当你点击action按钮,也会执行上面一样的方法。

所以如果你想要程序在push notification到达时,针对前台和后台运行做区分处理,你可以在上面方法里先做一个状态的

判断:

view plain
  1. //可以根据application状态来判断,程序当前是在前台还是后台  
  2. UIApplicationState state = [application applicationState];  
  3. if (state == UIApplicationStateInactive) {  
  4.       
  5.     // Application was in the background when notification  
  6.     // was delivered.  
  7. }  


你可能感兴趣的:(APPLE推送通知APNS)