Support Notification Interruption Level in Cloud Messaging in a better way
As you may know, Apple introduced UNNotificationInterruptionLevel last year that allow us to set the interruption level of a notification to active, passive, timeSensitive and critical.
At the moment, I was able to make it work with Firebase by adding a key interruption-level to the custom data and by extracting it from the request.content.userInfo like the following:
import UserNotifications
import FirebaseMessaging
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = request.content
.mutableCopy() as? UNMutableNotificationContent
guard let bestAttemptContent = bestAttemptContent else { return }
// Adding Notification Interruption Level support
if let rawInterruptionLevel = bestAttemptContent.userInfo["interruption-level"] as? String {
var interruptionLevel: UNNotificationInterruptionLevel = .passive
if rawInterruptionLevel == "1" || rawInterruptionLevel == "active" {
interruptionLevel = .active
} else if rawInterruptionLevel == "2" || rawInterruptionLevel == "timeSensitive" {
interruptionLevel = .timeSensitive
} else if rawInterruptionLevel == "3" || rawInterruptionLevel == "critical" {
interruptionLevel = .critical
}
bestAttemptContent.interruptionLevel = interruptionLevel
}
FIRMessagingExtensionHelper().populateNotificationContent(
bestAttemptContent,
withContentHandler: contentHandler)
}
I think this could be improved by adding and Interruption level picker in the "Additional options (optional)" instead of having to add it to the custom data like this:

A picker like for "Sound" or "Apple badge" would be ideal and make marketing people who are creating campaigns more happy ;-).
On the iOS SDK side, I noticed the FIRMessagingExtensionHelper().populateNotificationContent(... and I truly believe that it could be improved.
https://github.com/firebase/firebase-ios-sdk/blob/3512851cf669f2173f04b415e64a1ded020595a2/FirebaseMessaging/Sources/FIRMessagingExtensionHelper.m#L108
In fact, I believe that following code snippets logic should be part of it:
// Adding Notification Interruption Level support
if let rawInterruptionLevel = bestAttemptContent.userInfo["interruption-level"] as? String {
var interruptionLevel: UNNotificationInterruptionLevel = .passive
if rawInterruptionLevel == "1" || rawInterruptionLevel == "active" {
interruptionLevel = .active
} else if rawInterruptionLevel == "2" || rawInterruptionLevel == "timeSensitive" {
interruptionLevel = .timeSensitive
} else if rawInterruptionLevel == "3" || rawInterruptionLevel == "critical" {
interruptionLevel = .critical
}
bestAttemptContent.interruptionLevel = interruptionLevel
}
Again, this is just a temporary solution and you could restrict values if it was part of the SDK. Finally, that part of the SDK is still in Objective-C. Could be nice to have it in Swift (if it's not already in your roadmap scope ^^).
Hoping that this helps other devs who wants to implement this feature or that it contributes to improve the SDK.
Anyhow, Thank you, Marc
I found a few problems with this issue:
- I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
- This issue does not seem to follow the issue template. Make sure you provide all the required information.