【问题标题】:UNNotificationContentExtension does not update its default content footer when a new notification arrives新通知到达时,UNNotificationContentExtension 不会更新其默认内容页脚
【发布时间】:2019-06-21 08:42:45
【问题描述】:

我有一个通知内容扩展的实现,它使用 iOS 打开时提供的默认通知内容(底部的两个文本行):

问题是当一个新的通知到达时UNNotificationContentExtension 被打开页脚的标题和正文字符串没有得到更新。我检查了方法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];

【问题讨论】:

    标签: ios usernotifications usernotificationsui


    【解决方案1】:

    您需要实现与 UNNotificationContentExtension 相同的 UNNotificationServiceExtension 扩展。然后在 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)
            }
        }
    
    }
    

    【讨论】:

    • 我不想隐藏它,只想更新它的内容。
    • 方法didReceive(_:withContentHandler:)仅适用于UNNotificationServiceExtension...这是一个UNNotificationContentExtension。也没有有效负载 - 我在本地发送通知(APN 尚未激活)。
    • 您可以通过从通知正文访问来使用您的本地数据。如果你想改变通知的内容,那么你应该实现UNNotificationServiceExtension。请参阅此参考“developer.apple.com/documentation/usernotifications/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2020-10-28
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    相关资源
    最近更新 更多