检查推送通知注册:isRegisteredForRemoteNotifications不更新

以下方法保持返回相同的值:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; 

每次运行代码时,结果都是“是”。 即使当我进入“设置”应用程序,并将推送通知设置为“closures”我的应用程序,上面的代码运行时,它的计算结果为YES。

其他细节:*我正在运行的应用程序得到了一个iPhone的iOS 8.1.3 *我在Xcode 6.1中运行的应用程序,我有手机物理连接到我的机器

任何想法为什么“isRegisteredForRemoteNotifications”的值不会改变?

由于iOS 8注册了设备并提供了一个令牌,即使用户select了推送。

在这种情况下,当推送被发送时,推送不会呈现给用户,但是如果你的应用程序正在运行,它将获得有效载荷,所以你可以在应用程序运行时更新它。

要检查是否在iOS 8中启用了推送通知,您应该检查已启用的用户通知types:

 - (BOOL)pushNotificationsEnabled { if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) { UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; return (types & UIUserNotificationTypeAlert); } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; return (types & UIRemoteNotificationTypeAlert); } } 

如果你使用的是Swift 2,那么按位运算符将不能使用UIUserNotificationType。 这是一个使用Swift 2,iOS 8+的解决scheme:

 func hasPushEnabled() -> Bool { //ios 8+ if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") == true { let settings = UIApplication.sharedApplication().currentUserNotificationSettings() if (settings?.types.contains(.Alert) == true){ return true } else { return false } } else { let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes() if types.contains(.Alert) == true { return true } else { return false } }