Skip to content

Support Notification Interruption Level in Cloud Messaging in a better way #10124

Open
@marcgeeklaberge

Description

@marcgeeklaberge

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:
Screen Shot 2022-08-21 at 10 20 42 AM

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.

- (void)populateNotificationContent:(UNMutableNotificationContent *)content

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions