UIUserNotificationSettings不能正常工作?

我有下面的代码,我用来决定用户是否已授予本地通知的权限:

UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; if (grantedSettings.types == UIUserNotificationTypeNone) { NSLog(@"No permission granted"); //IF AND ONLY IF alerts are wanted by raymio user! FIX UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Notice" message:@"You need to enable notifications to be able to receive sun alerts!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* OKAction = [UIAlertAction actionWithTitle:@"Go to Settings now" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:url]; }]; [alert addAction:OKAction]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:cancelAction]; [self.window addSubview:self.window.rootViewController.view]; [self.window makeKeyAndVisible]; [self.window.rootViewController presentViewController:alert animated:YES completion:nil]; } else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){ NSLog(@"Sound and alert permissions "); } else if (grantedSettings.types & UIUserNotificationTypeAlert){ NSLog(@"Alert Permission Granted"); 

基本上,它只是不工作。 当允许通知开关在应用程序设置中明确设置为closures时,我会得到“警示许可”授予。 如果我把它打开,设置不会改变。 以下是授权设置的控制台输出。 它保持不变。 我还有其他的情况是closures的。 现在我正在诉诸于删除代码。 如果用户不小心按下了初始提示中的取消(并且在应用程序中请求提醒),我必须首先执行此操作。

 granted settings: <UIUserNotificationSettings: 0x17042e940; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> 

在这个越野车领域的任何洞察力? 顺便说一句,我正在运行8.1。 在ios9上这个需求并不完全一样,因为ios9会让用户多次提示通知许可。

我在我的一个项目中使用了下面的方法来确定用户是否授予了权限,或者他/她是否真的closures了设置中的通知。 把这个方法放在Appdelegate中并检查

 -(BOOL)notificationServicesEnabled { BOOL isEnabled = NO; if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) { isEnabled = NO; } else { isEnabled = YES; } } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types & UIRemoteNotificationTypeAlert) { isEnabled = YES; } else{ isEnabled = NO; } } return isEnabled; } 

然后你可以简单地检查条件

 if([self notificationServicesEnabled]) 

我希望这应该帮助你。

在iOS 9中,为了检查应用程序当前是否注册了远程通知,请使用以下命令:

 BOOL isEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];