React Native集成FCM后安卓模拟器无法接收测试通知求助
问题排查:React Native 集成 FCM 安卓通知接收失败
一、依赖配置冲突
你的android/build.gradle中存在重复且版本不一致的google-services依赖:
buildscript块中声明了classpath 'com.google.gms:google-services:4.3.8'allprojects块中又重复声明了classpath('com.google.gms:google-services:4.4.1')
修复方案:
只保留一处声明,统一版本,建议保留在buildscript的dependencies中,删除allprojects下的重复依赖:
buildscript { repositories { ... google() } dependencies { ... classpath 'com.google.gms:google-services:4.4.1' classpath('com.google.firebase:firebase-crashlytics-gradle:2.7.0') } } allprojects { ... repositories { ... google() ... } // 删除此处的google-services依赖 }
二、AndroidManifest 配置问题
- 过时接收器冗余:你配置的
com.google.android.gms.gcm.GcmReceiver是旧GCM组件,FCM集成无需手动声明该接收器,建议删除整个<receiver>节点。 - 通知通道未初始化:安卓8.0(API 26)及以上必须创建通知通道,仅配置meta-data占位符无效。需在代码中主动初始化通道:
在PushController的componentDidMount中添加:
import { Platform } from 'react-native'; // ...其他代码 PushNotification.createChannel( { channelId: "app-notification-channel", // 自定义通道ID channelName: "应用通知", channelDescription: "接收应用推送通知", playSound: true, soundName: "default", importance: 4, vibrate: true, }, (created) => console.log(`通知通道创建状态:${created}`) );
三、PushController 代码问题
- 跨平台方法调用错误:
notification.finish(PushNotificationIOS.FetchResult.NoData)是iOS专属方法,安卓环境下调用会阻断onNotification逻辑,需区分平台处理:
onNotification: function (notification) { console.log("NOTIFICATION:", notification); // 仅在iOS环境调用finish方法 if (Platform.OS === 'ios') { notification.finish(PushNotificationIOS.FetchResult.NoData); } },
- senderID 校验:确认
senderID为Firebase项目的正确项目编号(可在Firebase控制台项目设置中查看)。
四、PushNotifications.js 代码问题
- 未使用的权限请求函数:
requestUserPermission函数调用了messaging()但未导入依赖,且从未执行。若使用react-native-push-notification的requestPermissions: true配置,已自动处理权限请求,可直接删除该函数;若需保留,需补全导入并调用:
import messaging from '@react-native-firebase/messaging'; import { useEffect } from 'react'; const PushNotifications = () => { async function requestUserPermission() { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (enabled) { console.log("授权状态:", authStatus); } } useEffect(() => { requestUserPermission(); }, []); // ...其他代码 }
- FlatList 渲染错误:函数组件中不能使用
this._renderItem,需将_renderItem改为组件内部的箭头函数:
const PushNotifications = () => { const _renderItem = ({ item }) => ( <View key={item.title}> <Text style={styles.title}>{item.title}</Text> <Text style={styles.message}>{item.message}</Text> </View> ); // ...其他代码 <FlatList data={pushData} renderItem={_renderItem} keyExtractor={(item) => item.title} /> }
五、模拟器环境问题
- 确保模拟器含Google Play服务:使用带
Google APIs标签的安卓模拟器镜像(如Pixel 5 API 33 Google APIs),打开模拟器后检查Google Play服务状态(设置-应用-查看Google Play服务)。 - 开启应用通知权限:在模拟器设置-应用-你的应用-通知中,确认通知权限已开启。
- 关闭电池优化:在设置-电池-电池优化中找到你的应用,设置为「不优化」,避免后台通知被拦截。
六、FCM 测试通知 Payload 格式
确保发送的通知Payload格式正确:
- 通知消息(前台/后台由系统自动展示):需包含
notification字段
{ "to": "你的FCM注册令牌", "notification": { "title": "测试通知", "body": "这是一条测试推送消息" }, "data": { "customKey": "customValue" } }
- 数据消息(需手动处理展示):仅含
data字段,需在onNotification中手动触发本地通知:
onNotification: function (notification) { console.log("NOTIFICATION:", notification); PushNotification.localNotification({ title: notification.data.title, message: notification.data.body, channelId: "app-notification-channel" }); if (Platform.OS === 'ios') { notification.finish(PushNotificationIOS.FetchResult.NoData); } },
内容的提问来源于stack exchange,提问作者AranAli
相关产品推荐
相关产品推荐

