Check user settings for push notification in Swift

Viewed 15749

I have push notifications in my app. Whenever the app is launched, I would like to check whether the user has enabled push notification for my application.

I do it this way :

let notificationType = UIApplication.sharedApplication().currentUserNotificationSettings()!.types
if notificationType == UIUserNotificationType.None {
    print("OFF")
} else {
    print("ON")
}

If push notifications are disabled by the user, is there any way to activate this from my app?

Or is there any alternative to send user to the push notification settings (Settings - Notifications - AppName)?

6 Answers

Here is the most thorough way, as of iOS12 and Swift 5:

 _ = UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        switch settings.authorizationStatus {
        case .notDetermined:
            <#code#>
        case .denied:
            <#code#>
        case .authorized:
            <#code#>
        case .provisional:
            <#code#>
        @unknown default:
            <#fatalError()#>
        }
    }
Related