Implementing iOS Critical Alerts
iOS 12 has the critical alerts feature added. The feature allows apps to send the critical alerts which play a sound even if Do Not Disturb is on or iPhone is muted. Not all apps are allowed to send critical alerts. Only the apps approved for critical alerts entitlement are allowed to send critical alerts. There is a talk on user notifications improvements at Apple’s WWDC 2018 which talks about critical alerts as well. The critical alerts entitlement request should be submitted at https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/ . The critical alert feature request requires the following information to be submitted.
Apple will review it and approve the request based on the information submitted. The approval looks like
After the approval, we need to generate a new provisioning profile on developer account with critical alerts entitlement enabled for the app id.
Download and install the manually generated provisioning profile. From now on you should start using the new provisioning profile in Xcode.
Just changing the provisioning profile in Xcode didn’t work for me. I had to add a the critical alert’s entitlement in my apps entitlement file.
That’s all the settings needed. Jumping to the code, a new critical alert authorization for notifications should be requested.
var authOptions: UNAuthorizationOptions?
if #available(iOS 12.0, *) {
authOptions = [.alert, .badge, .sound, .criticalAlert]
} else {
authOptions = [.alert, .badge, .sound]
}
UNUserNotificationCenter.current().requestAuthorization(options:
authOptions!) { (granted, error) in
if !granted {
print(“The application requires Notifications permission to
display push notifications. Please enable it in settings.”)
}
}
The new critical alert authorization asks the user for Critical alerts notifications approval.
Adding the sound dictionary in the apns message payload will generate the critical alert on the device.
{
“aps” : {
“sound” : {
“critical”: 1,
“name”: “critical-alert-sound.wav”,
“volume”: 1.0
}
}
}
Critical Alerts with legacy FCM:
The legacy FCM Server Protocol message pay load supports only the iOS sound key as string. It doesn’t support sound dictionary. Hence, I had to come with with a workaround to implement the critical alerts feature with legacy FCM.
My app was already using the iOS Notification Extension. The Notification Extension gets a chance to modify the notification message before it gets displayed to the user. Our server encrypts the push notification payload and the Notification Extension in the iOS app decrypts the push notification data before displaying the notificaiton to the user. This way we were able to implement the End-to-End encrypted push notifications.
The workaround was to rely on iOS Notification Extension to mark a push notifications as critical before displaying the notification to the user. The server sends a critical
flag set to 1 in the data part of the FCM message. The Notification Extension checks for the critical flag in the push notification payload and marks the notification as critical if it’s set to 1. The code for Notification Extension to mark a push notification as critical is in my stack over flow answer.