Flutter集成自定义iOS Notification Service Extension失效求助
我在Flutter应用中添加了自定义iOS Notification Service Extension,用于预处理Firebase Cloud Messaging(FCM)推送的通知,但扩展完全不生效,测试通知却可正常显示。由于使用Windows系统,我通过MacInACloud的Xcode添加了该扩展,生成了与Runner、Flutter同级的新文件夹。
Notification Service扩展代码
import UserNotifications 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 = "modified !" 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) } } }
AppDelegate代码
import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate } return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
FCM推送Payload
const payload = { "token":"evGbKFWA10fFoD2rsTUVOX:APA91bHfOZDkkcim2SjHB1-H5ldZu37OsE3G91s4xRYkbp4vK3PUodvNkzLurAONzxI5NUjf5tzmokfGUBu9avT4y_Uz3L6DpKJ_pMR43knNVctFAobtQupcdy_P_H58q1EtRU02TZ8D", "notification":{ "title":"Secret Message !", "body" : "Nothing", }, "content-available":0, "apns":{ "headers":{ "apns-priority":10, }, "payload": { "aps" : { "category" : "SECRET", "mutable-content" : 1, "alert" : { "title" : "Secret Message!", "body" : "(Encrypted)" }, }, "ENCRYPTED_DATA" : "Salted__·öîQÊ$UDì_¶Ù∞èΩ^¬%gq∞NÿÒQùw" }, } };
失效原因排查及解决方案
FCM Payload冲突:当同时指定顶层
notification字段和apns.payload时,FCM会优先使用notification字段的内容,这类推送会直接展示,不会触发Notification Service Extension。需要删除顶层的notification字段,仅保留apns.payload中的配置,确保推送触发扩展。AppDelegate委托配置错误:FlutterAppDelegate本身已经实现了
UNUserNotificationCenterDelegate协议,无需再手动赋值UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate,这行代码会导致委托设置异常,直接删除即可。Extension Bundle ID配置错误:Apple要求Notification Service Extension的Bundle ID必须是主App Bundle ID的子ID形式(例如主App为
com.example.myapp,Extension需为com.example.myapp.notification-service),检查Xcode中Extension的Bundle ID是否符合要求。Extension部署目标不一致:确保Extension的Deployment Target版本与主App保持一致,不能低于主App的版本,否则扩展无法正常触发。
Extension Info.plist配置缺失:检查Extension的
Info.plist,确认NSExtension字典配置正确:NSExtensionPointIdentifier必须为com.apple.usernotifications.serviceNSExtensionPrincipalClass需指向你的NotificationService类(格式为$(PRODUCT_MODULE_NAME).NotificationService)
依赖集成问题:在MacInACloud中打开项目时,需确保Extension的Target已集成必要依赖(如Firebase Messaging)。在Podfile中添加Extension的Target配置:
target 'NotificationServiceExtension' do use_frameworks! pod 'Firebase/Messaging' end执行
pod install后重新编译项目。测试环境限制:Notification Service Extension仅在真实iOS设备上生效,模拟器不支持该功能;同时需确保App处于后台或终止状态,前台推送由AppDelegate处理,不会触发扩展。
内容的提问来源于stack exchange,提问作者aloys-c

