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

Eth*_*ies 10 cocoa-touch ios usernotifications

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

现在不推荐使用此检查,我想更新它,但我无法弄清楚iOS 11中要使用的内容.

弃用警告如下:

在iOS 10.0中不推荐使用currentUserNotificationSettings:使用UserNotifications Framework - [UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:]和 - [UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]

我试图在这个警告的帮助下更新代码,但我无法弄清楚.

如果有人可以建议任何方式来获得这样的支票,那将会有很大帮助.我一直用于iOS 10的代码如下,谢谢.

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

McN*_*ght 24

步骤1 : import UserNotifications

第2步 :

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

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


Amr*_*ari 8

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

import UserNotifications
Run Code Online (Sandbox Code Playgroud)

我使用了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")
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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)
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

并称之为

  self.checkPushNotification {  (isEnable) in
    print(isEnable)
    // you know notification status.
}
Run Code Online (Sandbox Code Playgroud)