Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift ios check if remote push notifications are enabled in ios9 and ios10

How can I check if the user has enabled remote notifications on ios 9 or ios 10?

If the user has not allowed or clicked No I want to toggle a message asking if they want to enable notifications.

like image 477
user2636197 Avatar asked Nov 10 '16 15:11

user2636197


People also ask

Are push notifications enabled by default?

The key differentiator is that getting push notifications are the default for Android apps. For iOS, the user is prompted to allow notifications when they first open a new app, so the default is not getting push notifications for any given app.


1 Answers

Apple recommends to use UserNotifications framework instead of shared instances. So, do not forget to import UserNotifications framework. As this framework is new in iOS 10 it's really only safe to use this code in apps building for iOS10+

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

You may check official documentation for further information: https://developer.apple.com/documentation/usernotifications

like image 128
Ogulcan Orhan Avatar answered Oct 26 '22 22:10

Ogulcan Orhan