首页 > 解决方案 > 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

我错过了什么,我如何确保在收到推送时调用我修改后的扩展

标签: iosfirebasefirebase-cloud-messagingpush

解决方案


结果我需要将我的应用程序的部署目标与推送扩展的部署目标相匹配

在此处输入图像描述


推荐阅读