Check if user notifications are enabled after UILocalNotification is canceled

In my application, I want to check if the user has notifications turned on or not. In iOS 10, I did this using delegate validation.

This check is now outdated and I want to update it, but I can’t figure out what to use in iOS 11.

The failure warning is as follows:

currentUserNotificationSettings' deprecated in iOS 10.0: use Framework UserNotifications - [UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] and - [UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]

I tried updating the code with this warning, but cannot figure it out.

If anyone can offer in any case to receive a check similar to this work, this will help a lot. The code I used for iOS 10 is below, thanks.

let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == [] {
    print("Notifications are NOT enabled")
} else {
    print("Notifications are enabled")
}
+9
source share
3 answers

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 information.

+23
source

First step: -  You must add the header file as

import UserNotifications

I used the checkpushNotification method to check if the user includes a notification or not. Usage called this method from didFinishLaunchingWithOptions of the AppDelegate class. Hope this helps you if you have any problems, please 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")
            }
        }
    }

, , .

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.
}
+8

I think you can just call UIApplication.shared.isRegisteredForRemoteNotifications

0
source

Source: https://habr.com/ru/post/1655823/


All Articles