Deco Deco - 2 months ago 15
Swift Question

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()
}
}

Answer

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")
        }
        if let error = error {
            NSLog("Error: \(error.description)")
        }
    }

    UIApplication.shared.registerForRemoteNotifications()

You can check the settings using getNotificationSettings()

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