Requesting provisional authorization to send local user notifications comes with some caveats but avoids interrupting the user with another permissions prompt.
Explicit Authorization
Before sending user notifications you need permission from the user. For example, to request the ability to show alerts and set an App’s badge icon:
let options: UNAuthorizationOptions = [.alert, .badge]
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: options) { (granted, error) in
if granted {
// success
} else {
// failure
}
}
Note: I’m showing the completion handler versions of the API but there are also async versions.
The first time you request authorization the user sees the familiar system prompt:
Apple suggests you request authorization in a context where the user will understand why they are being prompted. In practise, this often happens quickly after first launch which is not the best user experience.
Provisional Authorization
Provisional authorization is not new. Apple added it back in iOS 12. That’s so long ago that I forgot it existed. It comes with some caveats but it does provide an alternative to interrupting your users with permission prompts.
To request provisional authorization add .provisional
to the options:
let options: UNAuthorizationOptions = [.alert, .badge, .provisional]
When you ask for provisional authorization the system doesn’t interrupt the user with a prompt. The notifications you send are delivered quietly. They appear in the notification center with buttons that allow the user to keep the notifications or turn them off completely:
The keep button gives the user the choice between delivering immediately, quietly to the notification center, or if they have it enabled a scheduled summary.
At this point, if you check the permissions for the app in the system settings you’ll see that it only has permission to deliver notifications quietly:
In the detailed notification settings, alerts are restricted to the notification centre and, even though we requested it, we do not have permission to badge the app icon:
You don’t get full permission to show alerts or badge the app icon unless the user changes their notifications settings. I suspect for most apps that’s a fair compromise.
Checking for Permission
One extra note, if you’re checking the authorization status before sending a notification make sure you take the provisional status into account:
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { settings in
if (settings.authorizationStatus == .authorized)
|| (settings.authorizationStatus == .provisional)
{
// permission granted
}
}
Do you use provisional authorization for your app’s notifications or always prompt for explicit permission?