【问题标题】:Check whether user notifications are enabled after UILocalNotification deprecation检查 UILocalNotification 弃用后是否启用用户通知
【发布时间】:2017-10-10 10:13:18
【问题描述】:

在我的应用中,我希望能够检查用户是否启用了通知。在 iOS 10 中,我使用委托中的检查来执行此操作。

此检查现已弃用,我想对其进行更新,但我不知道在 iOS 11 中要使用什么。

弃用警告如下:

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

我尝试在此警告的帮助下更新代码,但我无法弄清楚。

如果任何人都可以建议获得这样的支票,那将有很大帮助。我在 iOS 10 上使用的代码如下,谢谢。

let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == [] {
    print("Notifications are NOT enabled")
} else {
    print("Notifications are enabled")
}

【问题讨论】:

    标签: ios cocoa-touch usernotifications


    【解决方案1】:

    第 1 步:import UserNotifications

    第 2 步:

    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
      if settings.authorizationStatus == .authorized {
        // Notifications are allowed
      }
      else {
        // Either denied or notDetermined
      }
    }
    

    检查设置对象以获取更多信息。

    【讨论】:

    • 在 iOS 10 之前的版本中如何处理这个问题?
    • 您必须通过currentUserNotificationSettings 方法从UIApplication.shared 获取UIUserNotificationSettings 对象。
    • 如何将其转换为抛出 Bool() 的函数?
    【解决方案2】:

    第一步:

    你必须将头文件添加为

    import UserNotifications
    

    我已经使用 checkpushNotification 方法来检查用户是否启用通知。使用从 AppDelegate 类的 didFinishLaunchingWithOptions 调用此方法。希望对你有帮助,如有问题请在下方评论。

    最后一步:

    func checkPushNotification(){
            if #available(iOS 10.0, *) {
                UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
                    
                    switch setttings.authorizationStatus{
                    case .authorized:
                        
                        print("enabled notification setting")
                        
                    case .denied:
                        
                        print("setting has been disabled")
                        
                    case .notDetermined:
                        print("something vital went wrong here")
                    }
                }
            } else {
    
                let isNotificationEnabled = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert)
                if isNotificationEnabled{
                    
                    print("enabled notification setting")
                }else{
                    
                    print("setting has been disabled")
                }
            }
        }
    

    如果你想启用或禁用某些布尔输出,那么你应该实现完成处理程序来解决它。

    func checkPushNotification(checkNotificationStatus isEnable : ((Bool)->())? = nil){
            
            if #available(iOS 10.0, *) {
                UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
                    
                    switch setttings.authorizationStatus{
                    case .authorized:
                        
                        print("enabled notification setting")
                        isEnable?(true)
                    case .denied:
                        
                        print("setting has been disabled")
                        isEnable?(false)
                    case .notDetermined:
                        
                        print("something vital went wrong here")
                        isEnable?(false)
                    }
                }
            } else {
                
                let isNotificationEnabled = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert)
                if isNotificationEnabled == true{
                    
                    print("enabled notification setting")
                    isEnable?(true)
    
                }else{
                    
                    print("setting has been disabled")
                    isEnable?(false)
                }
            }
        }
    

    并将这个简单称为

      self.checkPushNotification {  (isEnable) in
        print(isEnable)
        // you know notification status.
    }
    

    【讨论】:

    • 对于早期版本?
    • @Josh 我已针对上述早期版本进行了更新。如果它不起作用,请在下面发表评论。
    • @Amrit Tiwari 是否暗示如果 isNotificationEnabled = false,它已被拒绝?它的工作方式是否与您的示例中之前的 switch 案例相同?
    • isNotificationEnabled = false,这适用于用户被拒绝和未确定的两种情况。
    • 嗨,Amrit,你能告诉我如何将你的函数转换为 throw Bool() 作为结果吗?喜欢 -> Bool()?
    【解决方案3】:

    我想你可以打电话给UIApplication.shared.isRegisteredForRemoteNotifications

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 2011-08-27
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多