ios - FCM + iOS + 通知扩展
问题描述
我正在使用 FCM 实现 iOS 推送通知。但我想在通知中显示图像。这就是为什么我倾向于实施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
我错过了什么,我如何确保在收到推送时调用我修改后的扩展