Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable notifications for iOS 10?

In iOS9 we prompt our user if they'd like notifications and direct them to the settings page so they can enable notification permission for our app. However as shown below there is no option to enable notifications in my apps permissions settings within iOS 10. Is there a new process I must undertake to populate this list with the appropriate permissions?

enter image description here

Code

//check the notifications status. First the global settings of the device, then our stored settings
func checkGlobalNotifications() {
    let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
    if grantedSettings!.types.rawValue & UIUserNotificationType.Alert.rawValue != 0 {
        globalNotificationsEnabled = true
        //if global notifications are on, then check our stored settings (user)
        if CallIn.Settings.notificationsEnabled { notificationsOn() } else { notificationsOff() }
    }
    else {
        globalNotificationsEnabled = false
        //global notifications (iOS) not allowed by the user so disable them
        notificationsOff()
    }
}
like image 540
Declan McKenna Avatar asked Jul 25 '16 13:07

Declan McKenna


1 Answers

iOS 10 has introduced UNUserNotificationCenter, which is used now for all local and push notifications. For example:

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert])
    { (granted, error) in
        if granted == true{
            NSLog("Granted")
             UIApplication.shared.registerForRemoteNotifications()
        }
        if let error = error {
            NSLog("Error: \(error.description)")
        }
    }

You can check the settings using getNotificationSettings()

WWDC video: https://developer.apple.com/videos/play/wwdc2016/707/

like image 109
Gruntcakes Avatar answered Sep 28 '22 15:09

Gruntcakes