DPR
DPR

Reputation: 780

UNNotificationContentExtension does not update its default content footer when a new notification arrives

I have an implementation of a notification content extension which uses the default notification content (the two text lines at the bottom) provided by iOS when opened:

UNNotificationContentExtension with default content enabled

The problem is that when a new notification arrives while the UNNotificationContentExtension is opened the title and body strings of the footer do not get updated. I've checked that method didReceive() is being correctly invoked again and that the passed UNNotification has the correct updated information (parameters notification.request.content.body and notification.request.content.title). However, the OS appears to simply ignore them, leaving the text at the bottom unchanged even through we can update the content itself with no issue.

Is it possible to force the default content to update? There doesn't seem to be any parameter and/or method which we can use to do so...

Thank you in advance for any response.

EDIT: I should also add that the notifications are being generated locally (APN is not active yet). The code looks something like this:

UNMutableNotificationContent *notificationContent = [UNMutableNotificationContent new];
notificationContent.categoryIdentifier = @"my.notification.category";
notificationContent.title = @"My notification title";
notificationContent.body = @"My notification body";

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:notificationUUID
                                                                      content:notificationContent
                                                                      trigger:notificationTrigger];

UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
[notificationCenter addNotificationRequest:request withCompletionHandler:nil];

Upvotes: 1

Views: 1033

Answers (1)

Sagar Chauhan
Sagar Chauhan

Reputation: 5823

You need to implement UNNotificationServiceExtension extension same as UNNotificationContentExtension. Then in didReceive method your can access your payload and update it.

didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) will be called before notification content extension's method.

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)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

            contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

Upvotes: 0

Related Questions