Flutter集成FCM:Android端点击通知打开应用主页面问题排查
Android端Firebase通知点击无响应的修复方案
1. 配置AndroidManifest的主Activity Intent过滤
问题核心在于系统不知道点击通知后要唤起哪个页面。需在android/app/src/main/AndroidManifest.xml的主Activity(通常是MainActivity)中,添加匹配FLUTTER_NOTIFICATION_CLICK的intent-filter:
<activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- 原有启动页过滤规则 --> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <!-- 新增通知点击匹配规则 --> <intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
2. 可选:通过Data字段增强跳转控制
如果需要更灵活的页面跳转逻辑,可以在推送Payload中加入data字段,配合Flutter端的监听处理:
修改Cloud Functions代码:
.document('testdocument/{testdc}') .onCreate((snapshot, context) => { return admin.messaging().sendToTopic('test', { notification: { title: "APP MON", body: "Check Mon!!!", clickAction: 'FLUTTER_NOTIFICATION_CLICK', }, data: { target: "home" // 自定义标识,用于指定跳转页面 } }); });
在Flutter项目中监听通知点击事件:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { // 直接跳转到主页面 Navigator.pushReplacementNamed(context, '/'); });
3. 确认LaunchMode设置
主Activity的android:launchMode设为singleTop,避免点击通知时重复创建Activity实例,确保直接唤起已存在的主页面。
内容的提问来源于stack exchange,提问作者Alexandros Tziokas
相关产品推荐
相关产品推荐

