FCM + iOS +通知扩展

时间:2019-04-23 08:24:14

标签: ios firebase firebase-cloud-messaging push

我正在使用FCM实现iOS Push通知。但是我想在通知中显示图像。这就是为什么我倾向于实施UNNotificationServiceExtension

我要做的是以下几点。添加新的target> notification service extension。该目标当前包含

enter image description here

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)
        }
    }

}

然后,我发送带有以下负载的推送通知:

enter image description here

收到推送通知等,但是它没有通过扩展名,因为它不包含我在NotificationService中添加的修改后的数据

我缺少什么,如何确保收到推送后修改后的扩展名被调用

1 个答案:

答案 0 :(得分:0)

原来,我需要将我的应用程序的部署目标与Push Extension的部署目标相匹配

enter image description here