【问题标题】:Check if Local Notifications are enabled in IOS 8检查是否在 IOS 8 中启用了本地通知
【发布时间】:2014-11-21 00:22:33
【问题描述】:

我在整个互联网上查看了如何使用 IOS 8 创建本地通知。我找到了很多文章,但没有一篇文章解释了如何确定用户是否已将“警报”设置为打开或关闭。有人可以帮我吗!!!我更喜欢使用 Objective C 而不是 Swift。

【问题讨论】:

    标签: notifications ios8 localnotification


    【解决方案1】:

    您可以使用UIApplicationcurrentUserNotificationSettings查看它

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
        UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    
        if (grantedSettings.types == UIUserNotificationTypeNone) {
            NSLog(@"No permiossion granted");
        }
        else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
            NSLog(@"Sound and alert permissions ");
        }
        else if (grantedSettings.types  & UIUserNotificationTypeAlert){
            NSLog(@"Alert Permission Granted");
        }
    }
    

    希望对您有所帮助,如果您需要更多信息,请告诉我

    【讨论】:

    • 这很好用。如果用户之前拒绝了权限,则 type == none 但再次询问不会显示请求窗口。我怎么知道他们以前是否否认过?如果我已经问过,我应该在用户偏好中保留一个标志吗?
    • 请注意,它仅适用于 iOS 8+。
    • 注意,这只是确定它是否启用,而不是明确禁用。
    • 第二个if 不应该类似于 if (grantedSettings.types & (UIUserNotificationTypeSound | UIUserNotificationTypeAlert)),因为 7* (111) & 2 (010) & 4 (100) 之间的按位比较将导致 0. 7是所有权限
    • 本杰明所说的更进一步——(grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert) 永远不会是真的
    【解决方案2】:

    要扩展 Albert 的答案,您不需要在 Swift 中使用 rawValue。因为UIUserNotificationType 符合OptionSetType,所以可以执行以下操作:

    if let settings = UIApplication.shared.currentUserNotificationSettings {
        if settings.types.contains([.alert, .sound]) {
            //Have alert and sound permissions
        } else if settings.types.contains(.alert) {
            //Have alert permission
        }
    }
    

    您使用方括号 [] 语法来组合选项类型(类似于按位或 | 运算符用于组合其他语言中的选项标志)。

    【讨论】:

    • 如果您只想知道用户是否选择了“某物”,那么:let active = settings.types.intersection([.alert, .badge,.sound]).isEmpty == false
    • 你的答案是最好的答案 Michal Kaluzny +1
    【解决方案3】:

    Swift 与 guard:

    guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() where settings.types != .None else {
        return
    }
    

    【讨论】:

    • 你能解释一下你的代码是做什么的吗?如果 != .none { 什么? } 其他 { 什么? }
    【解决方案4】:

    这是 Swift 3 中的一个简单函数,用于检查是否至少启用了一种类型的通知。

    享受吧!

    static func areNotificationsEnabled() -> Bool {
        guard let settings = UIApplication.shared.currentUserNotificationSettings else {
            return false
        }
    
        return settings.types.intersection([.alert, .badge, .sound]).isEmpty != true
    }
    

    感谢 Michał Kałużny 的启发。

    【讨论】:

      【解决方案5】:

      编辑:看看@simeon 的answer

      在 Swift 中,你需要使用rawValue:

      let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
      if grantedSettings.types.rawValue & UIUserNotificationType.Alert.rawValue != 0 {
          // Alert permission granted
      } 
      

      【讨论】:

      • 不要使用rawValues,我认为不能保证它们永远不会改变。正如@simeon 所说,使用settings.types.contains(.Alert)
      • 不知道@ChrisWagner。我从 Matt Neuburg 的书中摘录了这个。我不做 iOS(1 年前我正在移植一个 Android 应用程序)。我已经编辑了答案以指向 simeon 的答案。无论如何,感谢您的反对:)
      【解决方案6】:

      我认为这段代码更精确:

      if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
      
          UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
      
          if (types & UIUserNotificationTypeBadge) {
              NSLog(@"Badge permission");
          }
          if (types & UIUserNotificationTypeSound){
              NSLog(@"Sound permission");
          }
          if (types & UIUserNotificationTypeAlert){
              NSLog(@"Alert permission");
          }
      }
      

      【讨论】:

      • 请考虑添加几行代码解释。
      【解决方案7】:

      使用@simeon 回答 Xcode 告诉我

      'currentUserNotificationSettings' 在 iOS 10.0 中已弃用:使用 UserNotifications Framework 的 -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] 和 -[UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]

      所以这是使用 Swift 4 的 UNUserNotificationCenter 的解决方案:

      UNUserNotificationCenter.current().getNotificationSettings(){ (settings) in
      
              switch settings.alertSetting{
              case .enabled:
      
                  //Permissions are granted
      
              case .disabled:
      
                  //Permissions are not granted
      
              case .notSupported:
      
                  //The application does not support this notification type
              }
          }
      

      【讨论】:

        【解决方案8】:

        目标 C + iOS 10

          UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
          [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        
                switch (settings.authorizationStatus) {
                    case UNAuthorizationStatusNotDetermined:
        
                        break;
        
                    case UNAuthorizationStatusDenied:
        
                        break;
        
                    case UNAuthorizationStatusAuthorized:
        
                        break;
        
                    default:
                        break;
                }
            }];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-16
          • 1970-01-01
          • 2016-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多