Check whether user notifications are enabled after UILocalNotification deprecation

旧巷老猫 提交于 2019-12-05 01:29:21

Step 1 : import UserNotifications

Step 2 :

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

Inspect the settings object for more informations.

First step:- You have to add header file as

import UserNotifications

I have used checkpushNotification method to check whether user enable notification or not. Uses called this method from didFinishLaunchingWithOptions of AppDelegate class. Hope It will help you, If any problem then comment below.

Last step:-

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

And if you want certain boolean output for is enabled or disabled then you should implement completion handler to solve it.

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

and Call this simple as

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

I think you can just call UIApplication.shared.isRegisteredForRemoteNotifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!