react-native-fcm遇ClassNotFoundException,接收Firebase通知时应用崩溃求助
排查React Native Firebase推送通知崩溃问题(基于react-native-fcm)
Hey Diego, sorry to hear your app's crashing when receiving Firebase push notifications—let’s break this down step by step. The fact that getFCMToken() works means your basic FCM setup is partially functional, so we can narrow down the issue to configuration gaps or notification handling logic.
1. Start with the Crash Log (Most Critical)
First, share the full crash stack trace from Android Studio’s Logcat (specifically the FATAL EXCEPTION section). This will tell us exactly what’s failing—common culprits include:
ClassNotFoundException(missing FCM-related native classes)NullPointerException(trying to parse a missing field in the push payload)SecurityException(mismatched app signatures or missing permissions)
2. Validate Core Configuration Files
Let’s check the key files you mentioned for common mistakes:
a. package.json Dependency Checks
- Confirm your
react-native-fcmversion is compatible with your React Native version (e.g., RN 0.60+ requires auto-linking support—if you’re using an olderreact-native-fcmversion, you might need to manually link or upgrade). - Run
npm list react-native-fcmoryarn list react-native-fcmto check for dependency conflicts with other libraries. - If you’re using newer Firebase tooling, ensure
@react-native-firebase/appis installed as a base dependency (even if you’re using the legacyreact-native-fcmlibrary).
b. Android Gradle Configs (build.gradle & app/build.gradle)
- Root
build.gradle:- Verify the Google Services plugin version is stable (e.g.,
classpath 'com.google.gms:google-services:4.3.10'—avoid overly new or outdated versions). - Ensure
google()is added to therepositoriesblock under bothbuildscriptandallprojects.
- Verify the Google Services plugin version is stable (e.g.,
app/build.gradle:- Make sure
apply plugin: 'com.google.gms.google-services'is placed at the very end of the file. - Check
minSdkVersionis ≥16 (FCM’s minimum requirement). - Confirm
compileSdkVersionandtargetSdkVersionare compatible with your Google Services plugin version. - Ensure
com.google.firebase:firebase-messagingis listed as a dependency, with a version matching the Google Services plugin.
- Make sure
c. AndroidManifest.xml
- Confirm FCM’s native services are declared:
<service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> - Verify required permissions are present:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> - Check that the
applicationtag includesandroid:name=".MainApplication"(points to your correct main app class).
d. MainApplication.java & MainActivity.java
MainApplication.java: EnsureFCMPackageis registered in thegetPackages()method:import com.evollu.react.fcm.FCMPackage; @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new FCMPackage() // This line is mandatory ); }MainActivity.java: If you’re handling incoming intents manually, make sureonNewIntentdoesn’t cause null pointers:@Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); // If using FCM's built-in handling, uncomment this: // FCMNotificationReceiver.onNewIntent(intent); }
3. Common Crash Causes & Fixes
- Payload Parsing Errors: If your push payload is missing fields like
titleorbody, your JavaScript code might throw a null pointer. Add safety checks:FCM.on(FCMEvent.Notification, async (notif) => { // Fallback to default values if fields are missing const title = notif.title || 'Default Notification'; const body = notif.body || 'New message'; // Handle the notification safely }); - Mismatched Signatures: Ensure the SHA-1 fingerprint in your Firebase Console matches the one used to sign your app (debug keystore for testing, release keystore for production).
- Initialization Conflicts: If other third-party libraries use Firebase, check for version conflicts in Gradle. Use
./gradlew app:dependenciesto audit dependency versions. - Auto-Linking Issues: For RN 0.60+, ensure you didn’t manually link
react-native-fcm(auto-linking should handle it—manual linking can cause duplicate class errors).
4. Next Steps to Debug
- Share the full crash stack trace from Logcat.
- Test a minimal push payload (only
titleandbody) to rule out payload-specific issues. - Run the app in debug mode with Android Studio’s debugger to inspect variables when the crash occurs.
- If possible, try creating a fresh RN project with
react-native-fcmto see if the issue reproduces (helps isolate project-specific config problems).
内容的提问来源于stack exchange,提问作者Diego Barranco
相关产品推荐
相关产品推荐

