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

iOS端Flutter应用Firebase getInitialMessage()终止状态返回null问题

Flutter iOS FCM终止状态跳转问题解决方案

问题核心原因

iOS终止状态下,FirebaseMessaging.instance.getInitialMessage()返回null且偶尔不稳定,本质是iOS对FCM推送payload的处理逻辑差异:

  • 当推送包含notification字段时,终止状态下iOS系统会直接接管通知展示,不会将完整消息传递给Flutter层,导致无法通过getInitialMessage()获取数据
  • content_available: true仅作用于应用后台的静默通知,对终止状态无效

解决方案

1. 调整FCM Payload结构

修改Node.js服务端的推送请求,通过调整payload适配iOS规则:

方案A:使用纯Data消息(推荐)

移除notification字段,所有跳转数据放入data中,iOS终止状态下会直接将消息传递给应用:

const response = await fetch("https://fcm.googleapis.com/fcm/send", {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${server_key}`,
  },
  body: JSON.stringify({
    registration_ids: tokens,
    data:{
      title: title,
      body: body,
      // 自定义跳转参数,如page: "order_detail", order_id: "1001"
    },
    content_available: true
  })
})

注意:纯Data消息不会自动在iOS通知栏展示,需Flutter端收到消息后调用FlutterLocalNotificationsPlugin手动弹出通知。

方案B:保留Notification字段并添加必要参数

若必须使用notification字段,需添加click_action: "FLUTTER_NOTIFICATION_CLICK",确保跳转数据能传递到Flutter层:

const response = await fetch("https://fcm.googleapis.com/fcm/send", {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${server_key}`,
  },
  body: JSON.stringify({
    registration_ids: tokens,
    notification:{
      title: title,
      body: body,
      click_action: "FLUTTER_NOTIFICATION_CLICK" // 必须配置
    },
    data:{
      // 自定义跳转参数
    },
    content_available: true,
    mutable_content: true // 可选,用于通知扩展功能
  })
})

2. 完善iOS原生端配置

确保AppDelegate.swift正确处理通知点击事件,将数据传递给Flutter:

import UIKit
import Flutter
import FirebaseMessaging

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    
    // 处理终止状态下启动应用的通知数据
    if let remoteNotification = launchOptions?[.remoteNotification] as? [String: Any] {
      Messaging.messaging().appDidReceiveMessage(remoteNotification)
    }
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  // 处理通知点击事件
  override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    Messaging.messaging().appDidReceiveMessage(userInfo)
    completionHandler()
  }
}

若FCM的getInitialMessage仍不稳定,可通过Deep Links实现跳转:

  • 在推送data字段中添加Deep Link地址,如link: "yourapp://product/100"
  • Flutter端使用uni_links插件监听Deep Links,点击通知后系统会直接打开Deep Link,插件捕获后跳转至对应页面

补充说明

  • iOS终止状态下的推送延迟或不稳定,可能是APNs服务器的推送延迟导致,调整payload结构后可大幅提升稳定性
  • 测试时必须使用真实iOS设备,模拟器对FCM推送的支持存在限制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:56:30