最近在开发过程中出现了一个问题,就是在iOS8.0系统上,我的应用在系统的设置->通知中心中不见了。这也就意味着系统认为我没有注册通知的需要,或者我的注册是失败的。
过去,我们一直使用这个方法进行通知中心的注册,并且一直没有出过问题。
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];
但是在新的文档中对于该段代码的解释却是这样的:
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types NS_DEPRECATED_IOS(3_0, 8_0, "Please use registerForRemoteNotifications and registerUserNotificationSettings: instead");
这也就是说认为它是Deprecated(过期)了的,并且推荐了新的API进行注册。
于是我们可以根据苹果的文档进行重新编写:(注意区分系统版本,否则会引起崩溃)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}