UNUserNotificationCenter
is the revamped notification framework, deprecating UILocalNotification
since iOS 10.
In WWDC 2018, there are some nice features in the framework, and here I will only be covering what’s new.
Provisional
Prior to iOS 12, an app has to explicitly ask user for permission before sending any notifications. Users will have to allow via a system alert like this:
With the provisional
option, the alert will NOT be shown!
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge, .provisional]) { ... }
This is great as users will automatically receive the notification, with the following tradeoff:
The notification will be delivered quietly, which means they will only appear in notification center, but NOT in the lock screen, present banners, update badge, or play sound.
This is good enough for many apps.
Subsequently, users can opt in to receive prominent notifications by going to iOS Settings and enable for Lock Screen and Banners, or when user choose Keep, they will be able to choose between quiet or prominent mode.
Or you can request authorization again, this time without provisional
.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { ... }
Critical Alert
All notifications are delivered silently if user enable Do Not Disturb mode.
All notifications are also delivered without sound if mute switch is on.
To override those, you can use the criticalAlert
option. But this require special entitlement, and you need to fill this form.
The kind of app Apple enable for are health and home security apps.
Custom App Settings Link
If your app provide your own UI settings for configuring notifications, you can provide the option providesAppNotificationSettings
so that iOS will link to your app UI in the following 2 places:
- In the “Manage” option when user receive a notification
- In iOS Settings > your app > Notification
MacKuba has the screenshots if you are unsure.
To provide your custom UI, implement in your app delegate:
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
// Display your custom UI
}
Other Basics
I have never cover the framework, and won’t be for now :) I have a guide on the basics to local notifications.
The Apple’s documentation is also pretty good, with the following topics: