1

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

启用默认内容的 UNNotificationContentExtension

问题是当打开新通知时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];
4

1 回答 1

0

您需要实现与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)
        }
    }

}
于 2019-01-28T11:17:27.737 回答