Pixel 2设备上FCM通知在应用被杀死后异常问题咨询
Hey there, let's tackle this FCM crash issue on your Pixel 2. I've seen similar problems before, and it's almost always tied to how your FirebaseMessagingService is implemented—especially when the app gets swiped away from the background stack.
First, Fix Your Service Declaration
Looking at your code snippet, you're using a hardcoded string in the IntentFilter: "com.google.firebase.MESSAGIN..." (looks like it's truncated). This is a common source of bugs. Instead of hardcoding the action, use the official constant provided by Firebase to avoid typos:
[Service] [IntentFilter(new[] { FirebaseMessagingService.IntentActionMessageReceived })] public class MyFirebaseMessagingService : FirebaseMessagingService { // Your implementation here }
Typos in the intent action mean the system can't route FCM messages to your service, which triggers crashes when the app is terminated (since FCM tries to start a service that doesn't properly listen for the right intent).
Why the Crash Happens When Swiping the App Away
When you swipe your app from the background stack, Android terminates its entire process. For FCM data messages (not notification messages), Firebase will attempt to restart your FirebaseMessagingService to deliver the message. If your service has uncaught exceptions (like null pointers, unhandled IO errors, or missing dependencies) in onMessageReceived or onNewToken, the service fails to start—triggering that "My App has stopped" error.
Once the system detects repeated crashes when starting the service, it will throttle or block future attempts to launch it, which is why you stop getting even the crash prompts later.
Google Official Guidelines & Best Practices
Here's what Firebase's official docs recommend for this scenario:
- Keep
FirebaseMessagingServicelightweight: Never do long-running work directly inonMessageReceived. UseWorkManageror a background coroutine to offload tasks—blocking the main thread here can cause the service to be killed by the system. - Catch all exceptions: Wrap all code in
onMessageReceivedandonNewTokenin try-catch blocks. Uncaught exceptions will crash the service, and Android will mark your app as unstable. - Understand message types:
- Notification messages are handled by the system tray when the app is terminated—they won't trigger your service unless you've set
click_action. - Data messages require your service to be running (or restarted) to process them. Make sure your service can handle these reliably.
- Notification messages are handled by the system tray when the app is terminated—they won't trigger your service unless you've set
- Test with Logcat: Use Android Studio's Logcat to filter for
AndroidRuntimelogs—this will show you the exact crash stack trace (like which line caused the exception). This is the fastest way to pinpoint the issue.
Debugging Steps to Try
- Check Logcat for crash details immediately after swiping the app away and sending a notification. Look for lines starting with
E/AndroidRuntime: FATAL EXCEPTION. - Test with a basic, stripped-down
FirebaseMessagingServiceimplementation (just log the message) to see if the crash still happens. If not, the issue is in your custom logic. - Ensure your app has all required FCM permissions and dependencies up to date (check the Firebase BOM version in your build.gradle/csproj).
内容的提问来源于stack exchange,提问作者Sankarann

