iOS9中如何注册远程通知

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)

在以往的版本中,我们可以通过:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:some types]

来注册远程通知,不过很快该方法就被标记为废弃,并且提示我们要用另一个类似的方法:

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

别高兴的太早,虽然registerUserNotificationSettings方法现在照样工作,不过其中的诸如UIRemoteNotificationTypeXXX之类的类型很快又被标记为废弃…

好吧,在iOS9中,注册远程通知变成了如下类似的代码:

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeBadge|UIUserNotificationTypeAlert categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

额,你没看错,以上代码在注册远程通知的同时还注册了本地通知,所以我们可以理解为远程通知只是在原有本地通知基础上开启了远程功能.不过这只是我个人的理解,欢迎纠错. ;)

你可能感兴趣的:(ios,注册,本地通知,远程通知)