Android应用接收FCM消息时前后台均崩溃问题求助
React Native FCM接收消息时崩溃问题
我开发的Android应用(首次React Native实践项目)在接收FCM消息时,无论处于前台运行还是后台状态都会崩溃。我无法确定问题根源,但推测可能与当前配置有关,相关配置代码如下:
android\build.gradle 配置
buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 firebaseVersion = "9.18.0" firebaseMessagingVersion = "18.8.0" // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP. ndkVersion = "23.1.7779620" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:7.3.1") classpath("com.facebook.react:react-native-gradle-plugin") classpath("com.google.gms:google-services:4.3.15") } }
AndroidManifest.xml 配置(路径:android\app\src\main\AndroidManifest.xml)
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.usapp"> <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- [START firebase_service] --> <service android:name=".java.MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <!-- [END firebase_service] --> </application> </manifest>
问题排查与解决建议
- 版本兼容性问题:你的
firebaseVersion(9.18.0)和firebaseMessagingVersion(18.8.0)版本差距过大,Firebase核心库与Messaging库版本必须严格兼容,这种版本不匹配是导致崩溃的常见原因。建议:- 移除单独的
firebaseVersion和firebaseMessagingVersion配置,改用Firebase BOM统一管理版本,避免手动指定版本冲突。在app模块的build.gradle中添加:implementation platform('com.google.firebase:firebase-bom:32.7.0') implementation 'com.google.firebase:firebase-messaging' - 同时升级
com.google.gms:google-services到最新兼容版本,比如4.4.0。
- 移除单独的
- 自定义Messaging服务问题:你注册了
.java.MyFirebaseMessagingService,但未提供该类的实现代码。如果该类存在未正确重写方法、空指针异常或未捕获的异常,会直接导致接收消息时崩溃。确保该类正确继承FirebaseMessagingService,并重写方法时做好异常处理:public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { try { // 处理消息逻辑 } catch (Exception e) { e.printStackTrace(); } } } - 查看崩溃日志:直接通过Android Studio的Logcat获取崩溃堆栈信息,定位具体的崩溃类和方法,这是最精准的排查方式,比如是否是某个依赖类找不到、空指针等。
内容的提问来源于stack exchange,提问作者Andras
相关产品推荐
相关产品推荐

