[英]UNNotificationContentExtension does not update its default content footer when a new notification arrives
我有一个通知内容扩展的实现,它在打开时使用 iOS 提供的默认通知内容(底部的两个文本行):
问题是当UNNotificationContentExtension
打开时UNNotificationContentExtension
新通知时,页脚的标题和正文字符串不会更新。 我已经检查过UNNotification
didReceive()
方法是否被再次正确调用,并且传递的UNNotification
具有正确的更新信息(参数notification.request.content.body
和notification.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.