iOS 8+始终启用远程通知function

对于iOS> = 8,仅限于

在我的AppDelegate,我注册用户通知如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"didFinishLaunchingWithOptions called"); // iOS >= 8.0 // register to receive user notifications [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; ... } 

完成后,我注册远程通知如下:

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { NSLog(@"didRegisterUserNotificationSettings called"); //register to receive remote notifications [application registerForRemoteNotifications]; } 

完成后,我会检查应用程序是否已注册

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken called"); BOOL pushNotificationOnOrOff = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; // always returns TRUE } 

但是,应用始终表示启用了推送通知,即使用户已经明确地设置了应用程序的远程通知function(通过首次安装应用程序后显示的通知权限警报或通过应用程序设置)。

我已经将应用程序的背景模式/远程通知设置为TRUE。 我一直在使用开发证书编译的实际设备(通过USB电缆连接)进行debugging。

帮忙,我已经打了几个小时了。

这似乎是一个错误,我也发现在iPhone 6,iOS 8.1.2的相同的行为。

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]总是返回TRUE即使用户拒绝推送通知权限(通过警报视图)或通过Settings.app > Notifications手动禁用Settings.app > Notifications

经过一番研究,我发现如果你为应用程序启用了Background App Refresh ,那么[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]将总是返回TRUE

Background App Refresh设置为FALSE[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]返回正确的值。

作为解决方法,您可以评估[[UIApplication sharedApplication] currentUserNotificationSettings].types来确定是否允许推送通知。

 typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) { UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received } NS_ENUM_AVAILABLE_IOS(8_0); 

希望这可以帮助。