3

Consider the following scenario:

App Installed
App Starts
Push Notification ➝ Request
Push Notification ➝ Don't Allow
App Closed or in Background
Settings ➝ User enabled Push Notification

How to get notified of this change without moving the app to foreground, so that the device token could be send to our backend server for receiving notifications even if the user do not open the app ?

I know that you can check whether notifications are allowed with UNUserNotifications framework like this:

static func checkNotificationStatus() {
    UNUserNotificationCenter.current().getNotificationSettings { settings in

        switch settings.authorizationStatus {
        case .authorized: generatePushToken()
        case .provisional: generatePushToken()
        case .denied: break
        case .notDetermined:
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
                checkNotificationStatus()
            }
        @unknown default: break
        }
    }
}

But that happens only after your app moves to foreground or it is launched again.

What to do if neither of those happen ?

Stoyan
  • 1,265
  • 11
  • 20
  • You don't need user permission to register for push notifications, only to show notifications (remote or local). Your app simply always registers for remote notifications and registers the token with your server. iOS will respect the users current notification settings for your app when the remote notification arrives – Paulw11 Feb 12 '20 at 07:51
  • I didn't know that, thanks mate will try it out! – Stoyan Feb 12 '20 at 07:53

0 Answers0