Flutter中FCM向多设备推送通知失败问题求助
FCM主题推送成功但设备收不到通知的排查方案
以下是针对问题的具体排查和解决步骤:
1. 确保所有用户设备已订阅目标主题
你在sendNotificationToAllUsers里添加的订阅代码,只会让当前调用该函数的设备订阅all主题,而非所有用户。需要在用户打开APP的初始化环节(比如首页的initState或main函数)添加订阅逻辑,确保所有活跃用户都加入该主题:
@override void initState() { super.initState(); FirebaseMessaging.instance.subscribeToTopic('all'); }
2. 验证FCM服务器密钥正确性
确认你使用的serverKey是Firebase控制台中的云消息传递服务器密钥,而非Web API密钥:
- 路径:Firebase控制台 → 项目设置 → 云消息传递 → 服务器密钥(注意区分旧版密钥,优先使用当前生效的服务器密钥)
密钥错误会导致接口返回200,但消息无法实际投递。
3. 配置Android通知渠道(Android 8.0+)
Android 8.0及以上系统要求必须创建通知渠道才能显示通知,可通过flutter_local_notifications插件完成配置:
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); // 初始化通知渠道 Future<void> initNotificationChannel() async { const AndroidNotificationChannel channel = AndroidNotificationChannel( 'default_channel_id', // 渠道ID 'Default Notifications', // 渠道名称 description: '用于接收系统通知', // 渠道描述 importance: Importance.max, ); await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(channel); }
在APP启动时调用该初始化函数,同时在FCM请求体中指定渠道ID:
var body = { 'notification': { 'title': 'Hey guys', 'body': 'How are you?!', }, 'android': { 'notification': { 'channel_id': 'default_channel_id' } }, 'to': '/topics/all', 'click_action': 'FLUTTER_NOTIFICATION_CLICK' };
4. 确认AndroidManifest配置位置正确
你添加的<intent-filter>必须放在处理通知点击的主Activity标签内部,示例如下:
<activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <!-- FLUTTER_NOTIFICATION_CLICK 过滤器需放在此处 --> <intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
5. 用Firebase控制台直接测试推送
前往Firebase控制台 → 云消息传递 → 发送测试消息,选择「主题」并输入all发送通知:
- 若设备能收到,说明你的代码请求存在问题;
- 若仍收不到,说明设备订阅或项目基础配置有异常。
6. 检查设备状态和权限
- 确认设备已开启APP的通知权限(设置 → 应用 → 你的APP → 通知);
- 检查设备是否处于免打扰模式;
- 分别测试APP在前台、后台、完全杀死状态下的通知接收情况。
7. 查看FCM投递日志
在Firebase控制台 → 云消息传递 → 消息报告中,查看你发送的消息的投递状态,排查“已发送但未送达”的具体原因。
内容的提问来源于stack exchange,提问作者Muhammad Qazmouz
相关产品推荐
相关产品推荐

