FCM showing duplicate notification when app is in

2019-01-28 07:33发布

问题:

I implemented FCM in my project. Push notification is working as expected, onMessageReceived is called when a notification is received. This is true when app is in the foreground.

However, when the app is in the background, the system tray always display a duplicate notification when one arrives (e.g. when Notification A is received, system tray display 2 Notification A)

How to fix this?

EDIT: added code

I extended FirebaseMessagingService class and have this in the onMessageReceived method

This is the only part in the project where I used NotificationManager.

Also, I tried to add a log on this method. onMessageReceived is called when app is in foreground. It doesn't get called when app is in background

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    RemoteMessage.Notification notification = remoteMessage.getNotification();

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String title = notification.getTitle();
        String message = notification.getBody();

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setContentIntent(pendingIntent);


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
}

回答1:

To handle push notifications manually use handleIntent(intent) of FirebaseMessagingService. This method gets called when application is in foreground, background and killed state. To avoid the duplication, do not call super.handleIntent(intent). This will prevent the automatic push notification when app in BG or killed state.

This worked for me.



回答2:

Same problem. I modified AndroidManifest.xml because i was requesting permissions to old GCM like this...

<uses-permission android:name="mypackage.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="mypackage.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

So, removing it from manifest and uninstalling and reinstalling the app my problem was solved.



回答3:

I was having the exact same 'duplicate' problem. This may be just a workaround because I couldn't get notifications behaving when app was in foreground without the 'duplicate' problem happening. Instead I implemented a WakefulBroadcastReceiver, when toggling the android:exported to "false" it started behaving.

AndroidManifest.xml
    <receiver
        android:name="PACKAGE.MyWakefulBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>

MyWakefulListenerService.java
    public class MyWakefulBroadcastReceiver extends WakefulBroadcastReceiver {

        private final String TAG = "MyWakefulListener";

        public void onReceive(Context context, Intent intent) {
            sendNotification(context);
        }
    }