我正在使用FCM实现iOS Push通知。但是我想在通知中显示图像。这就是为什么我倾向于实施UNNotificationServiceExtension
我要做的是以下几点。添加新的target
> notification service extension
。该目标当前包含
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]"
bestAttemptContent.subtitle = "Hey from extension"
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)
}
}
}
然后,我发送带有以下负载的推送通知:
收到推送通知等,但是它没有通过扩展名,因为它不包含我在NotificationService
中添加的修改后的数据
我缺少什么,如何确保收到推送后修改后的扩展名被调用