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

Flutter1.22.6点击FCM后台通知报pending intent canceled无法打开应用求助

问题排查与修复方案

你遇到的Notification pending intent canceled错误由以下几个可修复的配置问题导致:

核心问题1:重复调用configure方法覆盖回调

你在init方法中先调用了一次无参数的_firebaseMessaging.configure(),后续在configureNotifications中又重新创建FirebaseMessaging实例并再次调用configure,两次配置冲突会导致通知点击的回调被清空,系统找不到对应的处理逻辑就会取消 pending intent。

修复步骤:

  1. 移除init方法中的空参数_firebaseMessaging.configure()调用
  2. 统一使用全局声明的_firebaseMessaging实例,不要在configureNotifications中重新创建实例,修改后的configureNotifications方法开头改为直接使用已有的实例:
configureNotifications() {
  // 删掉这行重复实例化的代码:final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  _firebaseMessaging.configure(
    // 原有回调逻辑保持不变
  );
  // 剩余原有逻辑不变
}

核心问题2:FLUTTER_NOTIFICATION_CLICK意图过滤器位置错误

你当前将FLUTTER_NOTIFICATION_CLICK的intent-filter放在<application>标签下,该过滤器必须挂载到MainActivity标签下才能正确接收通知点击事件唤起应用,放错层级会导致系统找不到对应的处理页面,直接取消pending intent。

修复步骤:

调整AndroidManifest.xml结构,将intent-filter移动到MainActivity内部,示例结构如下:

<application
    android:name=".Application" 
    android:label="DR Vendor"
    android:requestLegacyExternalStorage="true"
    android:usesCleartextTraffic="true"
    android:icon="@mipmap/launcher_icon">
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        <!-- 原有MainActivity的其他配置保持不变 -->
        >
        <!-- 原有MainActivity的其他intent-filter(比如启动页的ACTION_MAIN过滤器)保持不变 -->
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <!-- 其他application下的配置保持不变 -->
</application>

其他辅助修复项

  1. 确认MainActivity的android:launchMode设置为singleTop,不要设置为singleInstance,否则无法正确传递通知点击的intent参数
  2. 补全onLaunch回调的跳转逻辑,当前onLaunch只有打印逻辑,应用完全杀死状态下点击通知启动时,需要和onResume一样调用onSelectNotification处理跳转:
onLaunch: (Map<String, dynamic> message) async {
  print("onLaunch: $message");
  onSelectNotification("${message['data']['order_id']}");
  return;
},
  1. 避免通知冲突:如果后台推送携带notification字段,firebase_messaging会默认展示系统通知,此时不要在myBackgroundMessageHandler中再次调用flutter_local_notifications创建重复通知,重复创建会覆盖pending intent导致取消。如果需要完全自定义通知样式,建议后台只发送data类型的推送,所有通知统一由flutter_local_notifications创建展示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 17:57:05