求助:解决Android无法启动RNPushNotificationRegistrationService服务问题
我之前也碰到过类似的头疼问题,折腾了好几个小时才搞定。结合你给出的错误日志和配置信息,咱们一步步排查:
核心问题定位
日志里的 W/ActivityManager: Unable to start service Intent ... RNPushNotificationRegistrationService not found 直接点明了关键:对应的推送注册服务没有在AndroidManifest.xml中正确注册,导致FCM无法触发token回调。
具体解决方案
1. 检查AndroidManifest.xml的服务注册
打开android/app/src/main/AndroidManifest.xml,确认以下内容是否存在(如果没有就手动添加):
<!-- 注册推送相关服务 --> <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <!-- 注册推送接收器 --> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationReceiver" /> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
注意:如果你的RN版本是0.60+,自动链接理论上会帮你加这些内容,但有时候会因为缓存或版本问题漏掉,手动补上最稳妥。
2. 修正Gradle配置的兼容性问题
(1)确保Google Services插件已添加
打开android/build.gradle,在buildscript的dependencies里加入Google Services插件:
buildscript { dependencies { // 其他依赖项... classpath 'com.google.gms:google-services:4.3.10' // 建议用这个稳定版本,避免兼容问题 } }
然后在android/app/build.gradle的末尾添加:
apply plugin: 'com.google.gms.google-services'
(2)指定具体的依赖版本
你当前用"+"来自动匹配版本,很容易出现Firebase和Google Play Services版本不兼容的情况。建议改成具体兼容版本:
ext { googlePlayServicesVersion = "17.0.0" firebaseVersion = "19.0.0" // 其他配置保持你的设置:compileSdkVersion、buildToolsVersion等 }
3. 清理缓存并重建项目
做完上述修改后,一定要清理缓存并重新构建,避免旧配置残留:
# 清理Android端缓存 cd android && ./gradlew clean # 返回项目根目录,重新安装依赖(可选) cd .. && npm install # 卸载设备上的旧应用,重新运行 react-native run-android
4. 检查基础配置细节
- google-services.json位置:确认文件放在
android/app目录下,且文件里的package_name和你应用的包名完全一致(包括大小写)。 - Sender ID正确性:代码里的
senderID必须是Firebase控制台「项目设置→云消息传递」里的Sender ID,不是项目ID。 - 代码适配Android端:你当前的代码里调用了
PushNotificationIOS.FetchResult.NoData,Android端不需要这个,建议改成平台判断:onNotification: function (notification) { console.log('NOTIFICATION:', notification); if (Platform.OS === 'ios') { notification.finish(PushNotificationIOS.FetchResult.NoData); } },
最后一步验证
如果以上步骤都做完,重新运行应用后,应该能在Logcat里看到TOKEN:的日志输出了。如果还是不行,可以试试降级react-native-push-notification到稳定版本(比如v8.1.1,这个版本和多数RN版本兼容性较好)。
内容的提问来源于stack exchange,提问作者Panda-Toine

