远程通知


  • 苹果给iOS和Mac添加了消息推送的功能,使得我们可以通过后台服务器给应用程序(APP)发送消息,不管APP是否正在使用,比如邮箱的来件提示功能。这项服务被称为Apple Push Notification service(APNs)。里面一共涉及到四个角色:APP、设备、APNs和应用后台服务器(Provider),其中APP、后台服务器和APNs之间使用deviceToken唯一的标识一个用户。

    \

    推送服务的工作流程:

    1. APP向系统注册推送服务。
    2. 设备从APNs请求deviceToken。
    3. 通过代理方法将deviceToken返回给APP。
    4. APP将deviceToken发送给应用后台服务器(Provider)。
    5. 应用后台服务器保存deviceToken,然后在需要推送通知的时候,给APNs发送信息,使用deviceToken标识所要送达的客户端。
    6. APNs将后台服务器发过来的数据推送到设备。
    7. 设备将消息分发给应用程序。

      在使用推送功能的时候,需要在开发者中心创建支持Push Notification的证书,并且将证书和私钥用于应用后台服务器与APNs之间通信。

      环境配置

      使用推送服务有一些必要条件:

      1. 开发者账号。
      2. iOS真机(iPhone、iPad、iPod)。
      3. 后台服务器。
      4. 网络。

        为了使应用支持推送服务,需要配置Provisioning Profile使它支持Push,和普通的Provisioning Profile文件一样分为Development和Production两个版本。我们使用Development版进行测试。

        \

        接下来创建一个用于应用后台服务器和APNs服务器通信时使用的SSL证书和私钥。

        1 .在钥匙串访问工具中获取证书请求文件(CSR)。

        \

        2 .保存请求文件。

        \

        3 .从钥匙串访问工具中导出私钥,将它保存为PushKey.p12,输入密码abcde。千万别把密码给忘了哈,等下要用的。

        \

        4 .登陆iOS Dev Center创建APP ID和*Provisioning Profile*。

        \

        \

        \

        5 .创建新的App ID时,要注意开启Push Notification

        \

        6 .最后App ID看起来是这样的。

        \

        7 .到这一步,虽然已经开启了推送服务,但是还需要进一步配置,点击Setting按钮进行设置。

        \

        8 .滚动到最下面,需要创建SSL证书(Create Certificate),测试环境使用Development SSL Certificate

        9 .查看证书创建步骤和说明,上传第1步得到的证书请求文件。

        \

        10 .下载生成好的证书,命名为aps_development.cer

        \


        到了这里,我们有多种选择继续了。

        1 .使用第三方小工具PushMeBaby模拟应用后台服务器发送推送信息。

        2 .搭建应用后台服务器发送推送信息。


        下面先试一试第一种方法,使用PushMeBaby。这是一个开源的Mac小程序,我们直接去Github下载源码,用Xcode打开,将ApplicationDelegate.m中天上deviceToken和证书的位置。

        01. <code class="language-objc">- (id)init {
        02. self = [super init];
        03. if(self != nil) {
        04. //77e231f0 76257e00 eed93ac6 47b52c78 12bae79f 9c9d1c67 4c990589 36c9a235 ---- 保留空格
        05. self.deviceToken = @"";
        06. //推送内容,JSON格式
        07. self.payload = @"{"aps":{"alert":"长沙戴维营教育iOS开发培训最好!","badge":1}}";
        08. //获取证书路径
        09. self.certificate = [[NSBundle mainBundle] pathForResource:@"aps_development" ofType:@"cer"];
        10. }
        11. return self;
        12. }
        13. </code>

        deviceToken的获取在下面的代码部分。

        一定要记得将刚才的证书文件添加到项目中,当然也可以直接将证书路径赋值给self.certificate。


        接下来试一试第二种方法,我们使用PHP来发送通知(其实运行PHP并不需要另外搭建服务器和下载程序,Mac默认支持PHP运行,不信到命令行运行一下php)。

        这种方式相对来说麻烦一些,但是也是实际使用的时候会采取的方式。我们需要进一步处理私钥和证书文件。

        1 .首先将证书文件和私钥处理成单个方便使用的pem文件,假设CSR、p12和cer文件都放在桌面上。

        1. <code class="language-objc"><code class="language-bash">$ cd ~/Desktop
        2. $ls
        3. aps_development.cer
        4. CertificateSigningRequest.certSigningRequest
        5. PushKey.p12
        6. </code></code>

        2 .将aps_development.cer转换为pem文件。

        1. <code class="language-objc"><code class="language-bash"><code class="language-bash">$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem
        2. $ ls
        3. aps_development.cer
        4. CertificateSigningRequest.certSigningRequest
        5. PushCert.pem
        6. PushKey.p12
        7. </code></code></code>

        3 .将p12私钥文件转换为pem文件。

        1. <code class="language-objc"><code class="language-bash"><code class="language-bash"><code class="language-bash">$ openssl pkcs12 -nocerts -out PushKey.pem -in PushKey.p12
        2. Enter Import Pass<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>:
        3. MAC verified OK
        4. Enter PEM pass phrase:
        5. Verifying - Enter PEM pass phrase:
        6. </code></code></code></code>

        4 .将两个文件合成同一个。

        01. <code class="language-objc"><code class="language-bash"><code class="language-bash"><code class="language-bash"><codeclass="language-bash">$ cat PushCert.pem PushKey.pem > ck.pem
        02. $ ls
        03. aps_development.cer
        04. CertificateSigningRequest.certSigningRequest
        05. ck.pem
        06. PushCert.pem
        07. PushKey.pem
        08. PushKey.p12
        09. </code></code></code></code></code>

        5 .测试证书是否有效。

        1. <code class="language-objc"><code class="language-bash"><code class="language-bash"><code class="language-bash"><codeclass="language-bash"><code class="language-bash">$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushCert.pem -key PushKey.pem
        2. </code></code></code></code></code></code>

        如果有效的话,会输出一堆信息,并且建立连接,否则不会成功建立连接。

        6 .使用PHP进行测试,下载SimplePush.php,修改文件并填入deviceToken和密码。在终端运行该代码。

        1. <code class="language-objc"><code class="language-bash"><code class="language-bash"><code class="language-bash"><codeclass="language-bash"><code class="language-bash"><code class="language-bash">$ php simplepush.php
        2. Connected to APNS
        3. Message successfully delivered
        4. </code></code></code></code></code></code></code>

        成功发送推送消息。

        代码实现

        有了上面的这些准备工作,iOS端的开发非常简单,UIApplicaton中有好几个方法都与推送消息有关,包括本地推送。

        01. <code class="language-objc"><code class="language-bash"><code class="language-bash"><code class="language-bash"><codeclass="language-bash"><code class="language-bash"><code class="language-bash"><code class="language-objc">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        02. //判断是否注册了远程通知
        03. if (![application isRegisteredForRemoteNotifications]) {
        04. UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
        05. [application registerUserNotificationSettings:uns];
        06. //注册远程通知
        07. [application registerForRemoteNotifications];
        08. }
        09.  
        10. return YES;
        11. }
        12.  
        13. //注册成功,返回deviceToken
        14. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
        15. {
        16. NSLog(@"%@", deviceToken);
        17. }
        18.  
        19. //注册失败
        20. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
        21. {
        22. NSLog(@"%@", error);
        23. }
        24.  
        25. //接收到推送消息
        26. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
        27. {
        28. NSLog(@"remote: %@", userInfo);
        29. }
        30. </code></code></code></code></code></code></code></code>

        甚至可以使用APNs实现一个聊天工具,具体请查看参考资料(4)。

        参考资料
        1. https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9
        2. https://github.com/stefanhafeneger/PushMeBaby
        3. http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
        4. http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2

           

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值