美文网首页iOS 开发 iOS Developer
iOS8以上registerForRemoteNotificat

iOS8以上registerForRemoteNotificat

作者: windyfat | 来源:发表于2016-05-28 09:39 被阅读1013次

    推送证书生成步骤:http://blog.csdn.net/showhilllee/article/details/8631734  
      参考链接:http://stackoverflow.com/questions/24454033/registerforremotenotificationtypes-is-not-supported-in-ios-8-0-and-later

    在注册苹果的推送通知的时候,我是这么写的:

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
                                                    (UIRemoteNotificationTypeBadge|
                                                     UIRemoteNotificationTypeSound|
                                                     UIRemoteNotificationTypeAlert)];
    

    运行没有报错,但是却打印出下面这句话,并且推送不成功:

    registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.
    

    原来iOS8以上的推送机制发生了变化,变成了互动式推送。所以为了满足所有的iOS系统版本,需要在注册推送通知后的时候进行判断,如果系统版本高于8.0,则换一种写法。代码如下:

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
                                                    (UIRemoteNotificationTypeBadge|
                                                     UIRemoteNotificationTypeSound|
                                                     UIRemoteNotificationTypeAlert)
                                                                                     categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        } else {
    
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(
                                                                                   UIUserNotificationTypeBadge |
                                                                                   UIUserNotificationTypeSound |
                                                                                   UIUserNotificationTypeAlert)];
        }
    

    这样写就OK了。

      补充内容:推送证书有两种,分为调试版本和发布版本,并且对应的网址也不相同。所以在将证书交给后台服务器的时候,分辨好是哪种证书,修改成对应的网址。否则是收不到推送的消息的。

    相关文章

      网友评论

        本文标题:iOS8以上registerForRemoteNotificat

        本文链接:https://www.haomeiwen.com/subject/jcpfdttx.html