安卓端使用FCM Notification Composer发送通知时收到重复通知问题
Hey there, let's tackle this duplicate FCM notification issue you're dealing with! From what you've shared, you're getting two notifications that only differ by their icon when sending via the FCM Notification Composer, and you've already tried solutions from the "FCM showing duplicate notification when app is in background" thread. Let's break down possible causes and actionable fixes based on the details you provided.
1. First, Audit Your Manifest Configuration
Let's start with your app's manifest—this is a common source of duplicate notifications.
Check for deprecated service registrations: If you're still declaring
FirebaseInstanceIdServicein your manifest, that might be part of the problem. Starting with Firebase SDK 17.0.0, this service is deprecated, and token refresh logic should live inFirebaseMessagingService'sonNewTokenmethod. Having both services registered can lead to conflicting handling of FCM events.
Example of what to remove from your manifest:<service android:name=".MyFirebaseInstanceIdService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service>Check default notification icon settings: If you've set a default notification icon via meta-data in the manifest, and also specified a different icon in the FCM Notification Composer, this could explain the icon difference in your duplicate notifications. Double-check this meta-data entry:
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_default_notification" />
2. Review Your FirebaseMessagingService Implementation
The most likely culprit here is duplicate notification sending in your custom service. Here's why:
- When your app is in the background, FCM automatically displays notifications that include a
notificationpayload (the ones you send via the Notification Composer). - If your
FirebaseMessagingService'sonMessageReceivedmethod is also manually creating and sending a notification for the same payload, you'll end up with two notifications.
Fix this by adding a check to only handle data payloads in onMessageReceived, and let the system handle notification payloads when the app is backgrounded:
@Override public void onMessageReceived(RemoteMessage remoteMessage) { // Skip handling if the message has a notification payload (let system handle it) if (remoteMessage.getNotification() != null) { return; } // Only process data payloads here and show your custom notification String title = remoteMessage.getData().get("title"); String message = remoteMessage.getData().get("message"); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, YOUR_CHANNEL_ID) .setSmallIcon(R.drawable.your_consistent_icon) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_DEFAULT); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(NOTIFICATION_ID, builder.build()); }
Also, make sure you're not hardcoding a different icon in your custom notification builder than the one set in the FCM Composer or manifest—this is exactly what would cause the two notifications to have distinct icons.
3. Double-Check FCM Notification Composer Settings
- Did you include both a
Notificationpayload and aDatapayload in your composer setup? If so, the system will display the notification payload, and youronMessageReceivedmight process the data payload to send another notification. Stick to one payload type unless you specifically need both. - Verify the icon you selected in the composer matches either your manifest's default icon or the one in your custom notification builder—this will eliminate the icon mismatch.
4. Quick Checks to Rule Out Edge Cases
- Ensure you don't have multiple
FirebaseMessagingServiceimplementations registered in your manifest (accidental duplicates happen!). - Check if any third-party libraries you're using are also handling FCM notifications—some analytics or push tools might hook into FCM and send their own notifications.
From your duplicate notification screenshot, the icon difference is a dead giveaway that one notification is coming from the system (using your manifest's default icon) and the other is coming from your custom service (using a different icon). Fixing the notification sending logic in onMessageReceived should resolve this.
内容的提问来源于stack exchange,提问作者Evan Sevy

