How to check notifications status for the iOS App

iOSApps/ApplicationsMobile Development

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 favourite team scores. Notifications can also tell your app to download information and update its interface. Notifications can display an alert, play a sound, or badge the app's icon.

You can read more about notification status here https://developer.apple.com/documentation/usernotifications

Apple recommend to user UserNotifications framework, So let’s get started. We will be seeing very simple and easy solution to get the notification status.

Step 1 − Firstly you need to import the UserNotifications framework

import UserNotifications

Step2 − Create an object of UNUserNotificationCenter.current()

let currentNotification = UNUserNotificationCenter.current()

Step 3 − Check status

currentNotification.getNotificationSettings(completionHandler: { (settings) in
   if settings.authorizationStatus == .notDetermined {
      // Notification permission is yet to be been asked go for it!
   } else if settings.authorizationStatus == .denied {
      // Notification permission was denied previously, go to settings & privacy to re-enable the permission
   } else if settings.authorizationStatus == .authorized {
      // Notification permission already granted.
   }
})

Final Code

import UserNotifications
let currentNotification = UNUserNotificationCenter.current()
currentNotification.getNotificationSettings(completionHandler: { (settings) in
   if settings.authorizationStatus == .notDetermined {
      // Notification permission is yet to be been asked go for it!
   } else if settings.authorizationStatus == .denied {
      // Notification permission was denied previously, go to settings & privacy to re-enable the permission
   } else if settings.authorizationStatus == .authorized {
      // Notification permission already granted.
   }
})
raja
Published on 24-May-2019 15:20:33
Advertisements