You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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-fcm version is compatible with your React Native version (e.g., RN 0.60+ requires auto-linking support—if you’re using an older react-native-fcm version, you might need to manually link or upgrade).
  • Run npm list react-native-fcm or yarn list react-native-fcm to check for dependency conflicts with other libraries.
  • If you’re using newer Firebase tooling, ensure @react-native-firebase/app is installed as a base dependency (even if you’re using the legacy react-native-fcm library).

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 the repositories block under both buildscript and allprojects.
  • app/build.gradle:
    • Make sure apply plugin: 'com.google.gms.google-services' is placed at the very end of the file.
    • Check minSdkVersion is ≥16 (FCM’s minimum requirement).
    • Confirm compileSdkVersion and targetSdkVersion are compatible with your Google Services plugin version.
    • Ensure com.google.firebase:firebase-messaging is listed as a dependency, with a version matching the Google Services plugin.

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 application tag includes android:name=".MainApplication" (points to your correct main app class).

d. MainApplication.java & MainActivity.java

  • MainApplication.java: Ensure FCMPackage is registered in the getPackages() 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 sure onNewIntent doesn’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 title or body, 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:dependencies to 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

  1. Share the full crash stack trace from Logcat.
  2. Test a minimal push payload (only title and body) to rule out payload-specific issues.
  3. Run the app in debug mode with Android Studio’s debugger to inspect variables when the crash occurs.
  4. If possible, try creating a fresh RN project with react-native-fcm to see if the issue reproduces (helps isolate project-specific config problems).

内容的提问来源于stack exchange,提问作者Diego Barranco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:14:45