簡體   English   中英

當新通知到達時,UNNotificationContentExtension 不會更新其默認的內容頁腳

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

我有一個通知內容擴展的實現,它在打開時使用 iOS 提供的默認通知內容(底部的兩個文本行):

啟用默認內容的 UNNotificationContentExtension

問題是當UNNotificationContentExtension打開時UNNotificationContentExtension新通知時頁腳的標題和正文字符串不會更新。 我已經檢查過UNNotification didReceive()方法是否被再次正確調用,並且傳遞的UNNotification具有正確的更新信息(參數notification.request.content.bodynotification.request.content.title )。 然而,操作系統似乎只是簡單地忽略了它們,即使我們可以毫無問題地更新內容本身,底部的文本也保持不變。

是否可以強制更新默認內容? 似乎沒有我們可以使用的任何參數和/或方法...

提前感謝您的任何回復。

編輯:我還應該補充一點,通知是在本地生成的(APN 尚未激活)。 代碼如下所示:

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];

您需要實現UNNotificationServiceExtension擴展一樣UNNotificationContentExtension。 然后在 didReceive 方法中您可以訪問您的有效負載並更新它。

didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent)將在通知內容擴展的方法之前調用。

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)
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM