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

Flutter集成FCM:iOS应用终止时无法接收数据消息求解决方案

关于iOS终止状态下FCM数据消息接收问题的求助

我正在开发一款集成FCM的Flutter应用,通知功能整体正常。为了根据特定条件接收消息,我采用数据消息而非通知消息,但遇到iOS应用处于终止状态时无法接收数据消息的问题,只有重新打开应用后才能恢复正常。

各平台表现

Android

  • 前台:运行正常
  • 后台:运行正常
  • 终止状态:运行正常

iOS

  • 前台:运行正常
  • 后台:运行正常
  • 终止状态:无法接收数据消息

FCM官方说明:iOS上若用户从应用切换器划走应用,需手动重启才能恢复后台消息接收。

我认为这是FCM特有问题,想请教是否有解决办法?

已执行操作

  • 使用真实设备而非模拟器测试
  • 以发布模式运行应用(flutter run --release)
  • 尝试使用「数据负载+通知」类型的消息
  • 未找到同类问题的有效解决方案

相关代码

BackgroundHandler代码(Dart)

@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {

  final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  final noticeNum = int.parse(message.data['noticeNum']);
  await loadButtonCheckedInfo();
  final switchButtonController = Get.put(SwitchButtonController());

  // Receive notifications only notification setting corresponding to noticeNum is true.
  if (switchButtonController.buttonCheckedList[noticeNum] == true) {    
      flutterLocalNotificationsPlugin.show(
      0,
      message.data["title"],
      message.data["body"],

      // notifications details setting (Android, iOS)
      const NotificationDetails(
        android: AndroidNotificationDetails(
          'high_importance_channel',              
          'high_importance_notification',         
          importance: Importance.max,
          priority: Priority.max,
          color: Color(0xFF3043f2),      
        ),
        iOS: DarwinNotificationDetails(
          presentAlert: true,
          presentBadge: true,
          presentBanner: true,
        )
      ),
    );
  }
}

消息推送服务端代码(Python)

def sendToFcm(token, title, body, noticeNum):            
    
    # Send to one device
    if len(tokenList) == 1:
        message = messaging.Message(
            data = {
                'title': title,
                'body': body,
                'noticeNum': noticeNum
            },
            token = tokenList[0],
        )

    # Send to many devices
    else:
        message = messaging.MulticastMessage(
            data = {
                'title': title,
                'body': body,
                'noticeNum': noticeNum
            },
            tokens = tokenList,      
        )

可能的解决方向

  1. 启用iOS后台模式权限
    确保在Xcode中开启「Remote notifications」后台模式,这是iOS接收后台/终止状态消息的基础权限。

  2. 调整消息推送优先级
    在服务端发送消息时,为iOS平台设置apns-priority为10(高优先级),同时添加content_available=True参数,告知iOS唤醒应用处理数据。修改后的服务端代码示例:

    # 单设备消息添加APNS配置
    message = messaging.Message(
        data = {
            'title': title,
            'body': body,
            'noticeNum': noticeNum
        },
        token = tokenList[0],
        apns=messaging.APNSConfig(
            headers={'apns-priority': '10'},
            payload=messaging.APNSPayload(
                aps=messaging.Aps(content_available=True)
            )
        )
    )
    
  3. 混合消息类型优化
    若使用「数据+通知」消息,iOS终止状态下会直接显示通知,只有用户点击通知才会触发应用启动并处理数据。如果需要自动处理数据,仍需依赖纯数据消息配合高优先级配置。

  4. 检查SDK与配置有效性
    确保firebase_messaging SDK为最新稳定版,同时验证iOS的GoogleService-Info.plist中APNs Environment配置与测试环境匹配。

  5. 验证APNs证书
    检查APNs推送证书是否有效、未过期,且正确关联应用ID,证书问题可能导致终止状态下消息无法送达。

内容的提问来源于stack exchange,提问作者김건혜

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 11:21:11