检查用户是否允许本地通知. iOS 8. Obj-C

前端之家收集整理的这篇文章主要介绍了检查用户是否允许本地通知. iOS 8. Obj-C前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果用户允许本地通知,是否没有简单的测试方法?在我拒绝发送本地通知后,我在控制台中发现了警告.当相关事件发生时,它表示该应用程序尝试发送通知,即使用户不允许它.我想在尝试显示通知之前检查是否允许它.在我的情况下看评论,我该怎么做?

我的代码是:

  1. app = [UIApplication sharedApplication];
  2. -(void)showBackgroundNotification:(NSString *) message {
  3. //check if app is in background and check if local notifications are allowed.
  4. if (app.applicationState == UIApplicationStateBackground /*&& LocalNotificationsAreAllowed*/){
  5. UILocalNotification *note = [[UILocalNotification alloc]init];
  6. note.alertBody = message;
  7. note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
  8. [app scheduleLocalNotification :note];
  9. }
  10. }

我得到用户提示,如下所示:

  1. UIUserNotificationSettings *settings;
  2. if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
  3. {
  4. settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotification TypeSound) categories:nil];
  5. [app registerUserNotificationSettings:settings];
  6. }

我不能使用设置对象?

编辑:我想我已经解决了.这似乎有效.

  1. -(void)showBackgroundNotification:(NSString *) message {
  2. if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){
  3. UILocalNotification *note = [[UILocalNotification alloc]init];
  4. note.alertBody = message;
  5. note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
  6. [app scheduleLocalNotification :note];
  7. }
  8. }

解决方法

这是我在不太具体的情况下使用的内容
  1. + (BOOL)notificationsEnabled {
  2. UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
  3. return settings.types != UIUserNotificationTypeNone;
  4. }

我通常在通知管理器中保留一组这些类型的方法.

猜你在找的iOS相关文章