检查UILocalNotification弃用后是否启用用户通知

在我的应用程序中,我希望能够检查用户是否启用了通知。 在iOS 10中,我在代理中使用了一个检查。

这个检查现在已经过时,我想更新它,但我不知道在iOS 11中使用什么。

弃用警告如下:

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

我试图用这个警告的帮助来更新代码,但我无法弄清楚。

如果任何人都可以build议这样的工作,这将有助于很多。 我一直在使用iOS 10的代码如下,谢谢。

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

第1步: import UserNotifications

第2步 :

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

检查设置对象的更多信息。

第一步: –你必须添加头文件为

 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") } } }