Swift how to detect when push notification permission is changed from system settings?
Asked Answered
P

2

6

User install the app and tapped "Don't allow" and denied push notifications. And when the app is active, user go to system settings and grant push notification and then go back to the application.

How can I detect that settings notification permission was changed when the application is going to active and call register for push notifications?

Pointblank answered 11/10, 2019 at 9:11 Comment(4)
You should always register for push notifications. You will get a call to didRegisterForRemoteNotificationsWithDeviceToken when the registration succeedsTavia
Check this answer : https://mcmap.net/q/217635/-swift-ios-check-if-remote-push-notifications-are-enabled-in-ios9-and-ios10Depressor
@Tavia I understand that but how can I detect that user change permission? Or every time when application applicationDidBecomeActive register pushes?Pointblank
You just register for push each time you launch. If, at any time, the user grants permission the didRegisterForRemoteNotificationsWithDeviceToken will be called.Tavia
F
4

For iOS 10.0 and later, you can use UNUserNotificationCenter.

You need to import this

import UserNotifications

and then user following to get notificAtion settings of your app.

let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in

    if(settings.authorizationStatus == .authorized) {
        print("Push notification is enabled")
    } else {
        print("Push notification is not enabled")
    }
}
Furtado answered 11/10, 2019 at 9:18 Comment(4)
Where should I call it?Pointblank
you can call this in AppDelegate to check that user has enabled notification or not.Furtado
But when user call application from background AppDelegate methods doesn't firedPointblank
you can this as per your requirement. Ideally we are calling this from AppDelegate's didFinishe... method. Based on your requirement you can call this as you need. Like in viewDidLoad of view controller class, etcFurtado
G
2

You can check it AppDelegates's didBecomeActive event by using same code. didBecomeActive event is called each time when app comes from background state to foreground state.

let center = UNUserNotificationCenter.current()

center.getNotificationSettings { (settings) in

if(settings.authorizationStatus == .authorized) {
    print("Push notification is enabled")
} else {
    print("Push notification is not enabled")
}

}

Gerhardt answered 11/10, 2019 at 10:37 Comment(1)
Use func sceneDidBecomeActive(_ scene: UIScene) inside SceneDelegate for newer versions of iOSFowling

© 2022 - 2024 — McMap. All rights reserved.