4

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?

Mahendra
  • 8,448
  • 3
  • 33
  • 56
Atlantis
  • 725
  • 7
  • 26
  • You should always register for push notifications. You will get a call to `didRegisterForRemoteNotificationsWithDeviceToken` when the registration succeeds – Paulw11 Oct 11 '19 at 09:17
  • Check this answer : https://stackoverflow.com/a/44407710/6059313 – Sharad Chauhan Oct 11 '19 at 09:19
  • 1
    @Paulw11 I understand that but how can I detect that user change permission? Or every time when application ```applicationDidBecomeActive``` register pushes? – Atlantis Oct 11 '19 at 09:22
  • 1
    You just register for push each time you launch. If, at any time, the user grants permission the `didRegisterForRemoteNotificationsWithDeviceToken` will be called. – Paulw11 Oct 11 '19 at 21:32

2 Answers2

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")
    }
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • Where should I call it? – Atlantis Oct 11 '19 at 09:23
  • you can call this in AppDelegate to check that user has enabled notification or not. – Mahendra Oct 11 '19 at 09:30
  • But when user call application from background AppDelegate methods doesn't fired – Atlantis Oct 11 '19 at 09:31
  • 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, etc – Mahendra Oct 11 '19 at 09:39
0

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

}

umer farooqi
  • 552
  • 2
  • 8