IOS 9不要求远程通知的权限

尝试询问远程通知的权限时遇到问题。

它在iOS 10上完美地工作,但是当我尝试在iOS 9设备上执行时,它不显示任何警报,并且不调用UIApplication委托方法“application:didRegisterForRemoteNotificationsWithDeviceToken:”。 既不是“失败”的方法。

我只testing真实的设备,而不是模拟器。 我目前用于请求权限的代码如下:

-(void)requestPushPermissions { NSLog(@"Starting register for remote Notification"); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) { [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"Got a yes!"); } else { NSLog(@"Got a no..."); } }]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } } 

任何人都有线索?

试试这个代码

 #import "AppDelegate.h" #define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self registerForRemoteNotification]; return YES; } - (void)registerForRemoteNotification { if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ if( !error ){ [[UIApplication sharedApplication] registerForRemoteNotifications]; } }]; } else { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } } 

在function中启用pushnotification