Check whether user notifications are enabled after UILocalNotification deprecation
Asked Answered
L

3

13

In my app, I want to be able to check if the user has notifications enabled or not. In iOS 10, I did this using a check in the delegate.

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

The deprecation warning is as follows:

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

I've tried to update the code with the help of this warning but I can't figure it out.

If anyone can suggest anyway to get a check like this working it would help a lot. The code I have been using for iOS 10 is below, thanks.

let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == [] {
    print("Notifications are NOT enabled")
} else {
    print("Notifications are enabled")
}
Lelea answered 10/10, 2017 at 10:13 Comment(0)
I
37

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.

Internalize answered 10/10, 2017 at 10:22 Comment(3)
how do you handle this in pre iOS 10?Heterophony
You would have to get a UIUserNotificationSettings object from UIApplication.shared via the currentUserNotificationSettings method.Internalize
How do you convert this into a func which throws Bool()?Fabriane
S
9

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.
}
Satyriasis answered 10/10, 2017 at 10:22 Comment(9)
and for earlier versions?Heterophony
@Heterophony i have updated for earlier version above. If it did not work please comment below.Satyriasis
@ Amrit Tiwari does that imply that if isNotificationEnabled = false, it has been denied? does it work the same way as in the earlier switch case in your example?Heterophony
isNotificationEnabled = false, this is for both case user denied and not determined.Satyriasis
Hi Amrit, could you show me how to convert your function to throw Bool() as result? like -> Bool()?Fabriane
@AmritTiwari are u able to share how did u throw a bool() here?Kamala
@Kamala i have updated answer please look and hope this will solved your problem. if any confusion feel free to ask.Satyriasis
@AmritTiwari thanks for the edits! do you know how I could getNotificationSettings callback to a synchronous method and return a Bool?Kamala
@Kamala why you want synchronous method?Satyriasis
D
-1

I think you can just call UIApplication.shared.isRegisteredForRemoteNotifications

Despite answered 19/9, 2019 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.