You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成FCM时Notification Service Extension未触发问题

问题:Notification Service Extension未触发,无法在通知展示前解密内容

需求背景

需要在通知横幅显示前更新内容(例如解密消息),该场景与Apple开发者文档的演示用例一致。原示例采用APNS Payload,已适配为FCM格式并通过Amazon SNS发送。

发送的FCM Payload

{
    "GCM": {
        "notification": {
            "title": "Sample message for Android endpoints",
            "body": "(Encrypted)",
            "mutable_content": true,
            "category": "SECRET"
        },
        "data": {
            "ENCRYPTED_DATA": "Salted__·öîQÊ$UDì_¶Ù∞èΩ^¬%gq∞NÿÒQùw",
            "imageUrl": "https://picsum.photos/200/300"
        },
        "aps": {
            "alert": {
                "title": "Title",
                "body": "Body"
            },
            "mutable-content": 1
        }
    }
}

Flutter接收通知的日志输出

flutter: FCM Category: SECRET
flutter: FCM Mutable Content: true
flutter: FCM Content Available: false
flutter: FCM Message Type: null
flutter: FCM Notification: Instance of 'RemoteNotification'
flutter: FCM Data: {ENCRYPTED_DATA: Salted__·öîQÊ$UDì_¶Ù∞èΩ^¬%gq∞NÿÒQùw, imageUrl: https://picsum.photos/200/300}

NotificationService.swift代码

//
//  NotificationService.swift
//  AppNotificationServiceExtension
//
//  Created by Rohit Kokate on 07/05/24.
//

import UserNotifications
import FirebaseMessaging

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)
        
        print("UNNotificationRequest : \(bestAttemptContent?.title ?? "default")")
        
        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
            
//            contentHandler(bestAttemptContent)
            Messaging.serviceExtension().populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler)

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

}

Podfile配置

在Podfile末尾为Extension添加了Firebase依赖:

target 'AppNotificationServiceExtension' do
  use_frameworks!
  pod 'Firebase/Messaging'
end

已确认的事项

  • 项目与Extension的Deployment Target版本一致
  • Build Phases中的“Embed Foundation Extension”未勾选“Copy only when installing”
  • Payload中已添加包含alert字段的aps字典
  • 已尝试重启测试设备
  • 查阅过同类问题但未解决

注:Firebase官方文档中关于Notification Service Extension的内容仅覆盖通知图片加载场景,未涉及解密这类自定义内容修改用例。

内容的提问来源于stack exchange,提问作者Rohit Kokate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 10:55:23