12

在我的应用程序中,我希望能够检查用户是否启用了通知。在 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")
}
4

3 回答 3

33

第1步 :import UserNotifications

第2步 :

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

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

于 2017-10-10T10:22:32.960 回答
9

第一步:

您必须将头文件添加为

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.
}
于 2017-10-10T10:22:59.873 回答
0

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

于 2019-09-19T21:37:19.990 回答