检测“允许通知”是iOS8的开启/closures

我正在尝试在iOS 8中检测App的本地通知设置

对于UIUserNotificationSettings ,它会返回我7,因为我打开了所有徽章,声音和警报。

在设置中,我closures“允许通知”,该应用程序仍然返回我为UIUserNotificationSettings(徽章,声音和警报)7。 有没有办法检测到“允许Notification ”开启/closures?

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ NSLog(@"---notificationSettings.types %d" , notificationSettings.types ); if(notificationSettings.types!=7){ UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Please turn on Notification" message:@"Go to Settings > Notifications > App.\n Switch on Sound, Badge & Alert" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; } } 

自iOS8以来,方法enabledRemoteNotificationTypes已被弃用。

要检查iOS8中的远程通知状态,您可以拨打电话

 [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; 

如果用户在设置中禁用通知,它将返回NO。 关于isRegisteredForRemoteNotifications 文档

或者您可以检索所有当前的通知设置:

 [[UIApplication sharedApplication] currentUserNotificationSettings]; 

有关currentUserNotificationSettings 文档

Swift 3+

 let isRegisteredForLocalNotifications = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert) ?? false 

Swift 2.3

 let isRegisteredForLocalNotifications = UIApplication.sharedApplication().currentUserNotificationSettings()?.types.contains(UIUserNotificationType.Alert) ?? false 

如果在错误的地方,我对这个回答/评论表示歉意。 我真的是在iOS编程新的,从来没有发布到堆栈溢出之前。 我认为这实际上应该是一个评论,但没有50评级,我是不允许的。 我也很抱歉,如果解释是有点简陋,但又一次,新的:)。

我也想testing一下,在最初的请求之后,用户是否已经改变了我的应用允许/要求的通知。 在试图破译苹果文档(苹果公司的作者要么比我更聪明,或者文档是故意模糊的)并且做了一些testing之后,我testing了它的价值

 [[UIApplication sharedApplication] currentUserNotificationSettings].hash. 

我相信这会返回一个三位散列值,其中第一位是Banner通知,第二位是声音通知,第三位是警报通知。

所以…

 000 = 0 = no notifications. 001 = 1 = only banner, 010 = 2 = only sound, 011 = 3 = sound and banner, no alert 100 = 4 = only alert notifications and so on until, 111 = 7 = all notifications on. 

如果“ 允许通知”在“设置”应用程序中处于closures状态,这也会显示0。 希望这可以帮助。

这应该是第一次启动你的应用程序时,用户将得到一个对话框,检查用户是否拒绝或允许通知,使用:

 -(void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings { if (notificationSettings.types) { NSLog(@"user allowed notifications"); [[UIApplication sharedApplication] registerForRemoteNotifications]; }else{ NSLog(@"user did not allow notifications"); // show alert here } } 

在连续启动时,使用:

 [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; 

要检查iOS8和iOS9中的远程通知状态,请致电:

 // Obj-C [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; // Swift UIApplication.sharedApplication().isRegisteredForRemoteNotifications 

如果您的应用可以接收远程通知,则返回true 。 但是, 接收远程通知并不意味着它也会将其显示给用户。

请求registerForRemoteNotifications()几乎在每种情况下都成功,并调用AppDelegate的didRegisterForRemoteNotificationsWithDeviceToken ,从而为您提供设备令牌。 然后,应用程序可以接收无法显示给用户的静音远程通知。 例如,您可以在收到此类通知时触发应用程序中的后台进程。 见文档 。

不仅要接收,还要向用户请求许可的远程通知:

 // Swift let notificatonSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(notificatonSettings) 

这将显示一个对话框,让用户可以允许或拒绝您的请求。 他们的决定漠不关心的应用程序仍然能够收到远程通知。

如果用户允许,则调用AppDelegate的didRegisterForRemoteNotificationsWithDeviceToken

要检查用户是否允许或拒绝了您的请求,或者实际上在iOS设置中更改了通知权限,请致电:

 // Obj-C [[UIApplication sharedApplication] currentUserNotificationSettings]; // Swift UIApplication.sharedApplication().currentUserNotificationSettings() 

见文档 。

您可以控制UIApplication.sharedApplication().currentUserNotificationSettings()的散列值。

 if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){ if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0){ pushNotificationStatus = "false" } else { pushNotificationStatus = "true" } } 

Swift 2

在AppDelegate中:

 func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { if (notificationSettings.types == .None){ // User did NOT allowed notifications }else{ // User did allowed notifications } } 

从任何其他ViewController:

  if UIApplication.sharedApplication().currentUserNotificationSettings()!.types.contains(.None){ }else{ } 

使用.isRegisteredForRemoteNotifications()不起作用(尽pipe它应该)。 但是,当通知被禁用或者其中一个types不存在时,下面的代码将工作。

 func notificationsAreOk() -> Bool { let wishedTypes = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound; let application = UIApplication.sharedApplication() let settings = application.currentUserNotificationSettings() if settings == nil { return false } if settings.types != wishedTypes { return false } return true } 

编辑 :一些testing后,并不总是工作时通知被禁用。 我正在考虑发送testing通知,以了解他们何时工作。

 UIUserNotificationType notificationType = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; if(notificationType == UIRemoteNotificationTypeNone) { NSLog(@"OFF"); } else{ NSLog(@"ON"); } 

为我工作

您可以使用以下代码检查用户设置是否启用推送通知。

启用或禁用Iphone推送通知

 UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types == UIRemoteNotificationTypeNone) // Yes it is.. 

希望对你有帮助..