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

Flutter项目iOS端App关闭后FCM data message无法接收问题求助

iOS端FCM纯数据消息杀死状态无回调解决方案

核心原因

iOS系统本身对推送有严格限制:纯data类型的静默通知,当App被用户从后台列表划掉强制终止后,系统默认不会主动唤醒App触发回调,只有携带notification字段的通知会由APNs直接展示,这是系统级的电池优化策略,并非插件配置错误导致。

解决方案步骤

1. 调整FCM推送请求体配置

需要在原有请求基础上补充APNs专属配置,否则iOS会拒收纯数据静默通知:

{
   "to": "/topics/global",
   "priority": "high",
   "content_available": true,
   "apns": {
      "payload": {
         "aps": {
            "content-available": 1
         }
      },
      "headers": {
        "apns-push-type": "background",
        "apns-priority": "5"
      }
   },
   "data": {
      "title": "Demo Title",
      "message": "Demo Message",
      "payload": "basic"
   }
}

注意:静默通知的apns-priority必须设为5,设为10会被系统直接拒收

2. 开启Xcode后台通知权限

  • 打开Flutter项目的iOS Runner工程,进入Signing & Capabilities标签页
  • 确认已开启Push Notifications能力,且APNs证书配置正确
  • 新增Background Modes能力,勾选Remote notifications选项

3. 补充AppDelegate回调方法

你当前的AppDelegate缺少静默通知的接收回调,且禁用Method Swizzling后需要手动初始化Firebase、转发消息给FCM插件,修改后代码如下:

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
   override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    // 手动初始化Firebase(禁用Swizzling后必须添加)
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
    }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  // 手动处理静默通知回调,转发给FCM插件
  override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    Messaging.messaging().appDidReceiveMessage(userInfo)
    completionHandler(.newData)
  }
}

4. 高可靠场景替代方案

受iOS系统限制,纯数据静默通知在App被杀死的场景下无法保证100%触发回调,如果你的业务要求必须所有场景都能收到通知且自行处理逻辑,可以采用以下方案:

  • 推送请求体保留notification字段
  • Xcode中新增Notification Service Extension扩展,拦截系统通知回调,自行修改通知样式、处理data字段逻辑,完全不需要依赖系统默认通知展示效果,可100%覆盖前台、后台、杀死所有场景。

内容的提问来源于stack exchange,提问作者Arifur Rahman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 23:12:02