Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether user notifications are enabled after UILocalNotification deprecation

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")
}
like image 828
Ethan Humphries Avatar asked Oct 10 '17 10:10

Ethan Humphries


People also ask

What are user notifications?

User-facing notifications communicate important information to users of your app, regardless of whether your app is running on the user's device. For example, a sports app can let the user know when their favorite team scores. Notifications can also tell your app to download information and update its interface.

How many local notifications can be scheduled IOS?

As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.


1 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 informations.

like image 142
Adam Avatar answered Oct 22 '22 16:10

Adam