自定义外观的Android通知:FCM使用及近三月失效问题求助
Hey there, let's dig into why your FCM data messages have stopped working over the last three months. I’ve put together a step-by-step troubleshooting checklist to help you narrow down the issue:
Check FCM SDK & Dependency Versions
Outdated SDKs often clash with Google’s updated FCM infrastructure. Pop into your build config files (likebuild.gradlefor Android) to confirm you’re using the latest stablefirebase-messagingversion. Don’t forget to check Google Play Services dependencies too—mismatched versions here are a common culprit for broken message delivery.Verify Device-Level Restrictions
Aggressive battery optimization or background app limits on many Android devices can kill the FCM connection. For example:- On Samsung: Head to Settings > Battery and device care > Battery > Background usage limits
- On Xiaomi: Check Settings > Battery & performance > App battery saver
Make sure your app is exempted from these restrictions—they can preventOnMessageReceivedfrom triggering when the app is in the background.
Validate Your Message Payload
Data messages rely entirely on thedatanode (no predefined "notification" key). Double-check your server-side payload to ensure it’s formatted correctly. A valid example looks like this:{ "to": "YOUR_DEVICE_TOKEN", "data": { "title": "Custom Alert", "body": "Your message content here", "custom_metadata": "any_value" } }Mixing notification fields with data fields can cause unexpected behavior, especially when the app is foregrounded vs. backgrounded.
Review FCM Console Logs & Reports
Jump into the Firebase Console’s Cloud Messaging section and check the Logs and Reports tabs. Look for delivery failures, invalid tokens, or quota errors. Expired device tokens (from app reinstalls or data clears) or hitting sending limits are frequent issues here.Test with a Minimal Payload
Send a stripped-down data message directly from the Firebase Console to a test device. If this works, the problem is likely in your server-side implementation (e.g., incorrect token handling, payload typos). Even console-sent messages failing points to device-side configuration issues.Confirm
OnMessageReceivedSetup
Make sure yourFirebaseMessagingServicesubclass is properly registered in your AndroidManifest.xml. The service declaration must include the correct intent filter:<service android:name=".YourMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>Also, avoid blocking the main thread in
OnMessageReceived—the system might kill the service before it can process the message.Check Network Access
Restricted networks (corporate proxies, VPNs) can block FCM’s endpoints. Ensure the device can reachfcm.googleapis.comand related domains. Test by switching to a different network (like home Wi-Fi) to see if messages come through.Review Recent App Updates
Think back to changes made around the time notifications stopped working. Did you add new permissions, modify background service behavior, or integrate a third-party library that could conflict with FCM? Roll back recent updates incrementally to spot a breaking change.
内容的提问来源于stack exchange,提问作者Haidar Hammoud

