Provisional Notifications - no device token

I have been following the official Apple documentation on how to set up notifications in my app:

https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications

I noticed there is this option .provisional, which doesn't ask the user for authorization, but instead it is granted right away and then notifications are delivered quietly to the notification center's history.

So far so good, but once I am granted authorization for sending .provisional notifications, the callback with the DeviceToken does not fire.

I have this method in my AppDelegate:

   func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  ) {
    let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    ManagementService.sharedInstance().storeDeviceToken(token: token)
  }

And it fires when authorization for normal notifications is granted, but it doesn't for the provisional notifications.

So the question is—how can I deliver the provisional notifications if I don't have the device token?

My plan was to do the following:

  • ask for authorization to send provisional notifications on first app launch and send the device token to my server
  • ask for authorization to send normal notifications some time later (let's say after a week or after completing some action in the app)

EDIT:

Found the problem—I was calling the same method to register for remote notification after being granted authorization for both provisional and normal notifications:

   func getNotificationSettings() {
   UNUserNotificationCenter.current().getNotificationSettings { settings in
     guard settings.authorizationStatus == .authorized else { return }
      
     DispatchQueue.main.async {
      UIApplication.shared.registerForRemoteNotifications()
     }
      
   }
  }

In this case settings.authorizationStatus == .authorized is false for provisional notifications (even though they are always authorized...). So I just omitted checking of settings.authorizationStatus for provisional notifications and the device token is there.

So it turned out, in that case settings.authorizationStatus is set to .provisional and not .authorized.

You should be able to request the device token irrespective of the authorization status. This allows 'silent' notifications aka 'content-available' notifications out of the box.

I'd just request the device token right away in didFinishLaunching.

Provisional Notifications - no device token
 
 
Q