React Native集成Firebase后发送通知致App崩溃,求排查方案
Hey there, I’ve run into a nearly identical issue with React Native + Firebase Cloud Messaging (FCM) before, so let me walk you through actionable troubleshooting steps that helped me resolve it:
Double-check Firebase SDK version consistency
Mismatched versions between@react-native-firebase/appand@react-native-firebase/messagingare one of the most common crash culprits. Ensure all your Firebase packages are on the same major version (e.g., ifappis v19,messagingshould also be v19). You can verify this in yourpackage.jsonor runnpm list @react-native-firebase/*in your terminal to cross-check versions.Audit your AndroidManifest.xml configurations
Missing or incorrect entries here often trigger crashes when notifications are received. Confirm you have these critical lines:- The FCM service declaration:
<service android:name="io.invertase.firebase.messaging.ReactNativeFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> - The default notification channel metadata (required for Android 8.0+):
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
Also, make sure the
default_notification_channel_idmatches the channel ID you create in your code or Firebase Console.- The FCM service declaration:
Validate notification channel setup
On Android 8.0+, notifications won’t deliver (and can crash the app) without a valid channel. Add this setup code to run when your app initializes:import messaging from '@react-native-firebase/messaging'; async function createNotificationChannel() { const channelId = 'default'; await messaging().setNotificationChannel(channelId, { name: 'Default Notifications', importance: messaging.AuthorizationLevel.HIGH, sound: 'default', }); } // Call this function early in your app's lifecycle createNotificationChannel();Test with a minimal notification payload
Complex payloads (like custom data fields with invalid types) can cause parsing errors. Send a stripped-down notification via Firebase Console: just a title and body, no custom data. If this works, gradually reintroduce your custom fields to pinpoint the problematic one.Dig into native error logs
The "pushNotif has stopped working" message is too vague. Connect your Android device to your computer and runadb logcat *:Eto filter for errors. Look for stack traces linked to Firebase Messaging or your app’s package name—this will show you the exact line of code or native module causing the crash.Re-link dependencies (for React Native < 0.60)
If you’re on an older React Native version, auto-linking might fail. Runreact-native link @react-native-firebase/appandreact-native link @react-native-firebase/messaging, then clean and rebuild withcd android && ./gradlew clean && cd .. && react-native run-android.Confirm notification permissions are granted
For Android 13+, you need to explicitly request thePOST_NOTIFICATIONSpermission. Add this check:import { PermissionsAndroid, Platform } from 'react-native'; async function requestNotificationAccess() { if (Platform.OS === 'android') { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS, { title: 'Notification Access', message: 'This app needs permission to send you notifications', buttonPositive: 'Allow', }, ); return granted === PermissionsAndroid.RESULTS.GRANTED; } return true; }
One final check: if you’re using a custom notification handler (like messaging().onNotification), scan for uncaught errors in that code. Even a small typo here can crash the app when a notification arrives.
内容的提问来源于stack exchange,提问作者Dror Bar

