Checking Push Notification Registration: isRegisteredForRemoteNotifications Not Updating

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

The following method keeps returning the same value:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

Every time this code runs, the results is YES. Even when I go into the "Settings" app and set push notifications to "off" for my app, when the code above runs, it evaluates to YES.

Other details: * I'm running the app on got an iphone that has iOS 8.1.3 * I'm running the app in Xcode 6.1 and I've got the phone physically attached to my machine

Any idea why the value of "isRegisteredForRemoteNotifications" doesn't change?

回答1:

Because iOS 8 does register the device and provides a Token even if the user opts out from pushes.

In that case pushes are not presented to the user when the push is sent, but if your app is running it gets the payload so you can update it when the app is running...

To check if push notifications are enabled in iOS 8 you should check for the enabled user notification 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);     } }


回答2:

If you're using Swift 2, the bitwise operators won't work with UIUserNotificationType. Here's a solution using Swift 2, iOS 8+ :

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             } }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!