7

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());
}
kishidp
  • 1,918
  • 6
  • 23
  • 29
  • 1
    Show how you are building and showing notifications – nipun.birla Feb 08 '17 at 10:28
  • 1
    when app is in background system generates notification automatically if it is in default notification format. – Vivek Mishra Feb 08 '17 at 10:32
  • @VivekMishra does that mean the system generates 2 of the same notification? or it receives 2 notification? I'm confused why when app is in foreground I only receive just the notification but when in background it suddenly becomes duplicated – kishidp Feb 08 '17 at 10:34
  • as you have not provided any code of yours, I suspect that one notification is generated by system and another by your code. – Vivek Mishra Feb 08 '17 at 10:36
  • @VivekMishra added the onMessageReceived code. this is the only part where I've used the NotificationManager notify method – kishidp Feb 08 '17 at 10:45
  • for testing purpose change message of the notification code and check whether that notification is shown or not – Vivek Mishra Feb 08 '17 at 10:59
  • Hello guyz, did you get solution to this, I am facing same issue, when app in foreground receiving one notification and app in background showing two notifications handled by systemtray. Can u help me to fix this issue. – sssvrock Aug 17 '17 at 07:05
  • i am also facing same issue . any one has solutions please ? – abdulec90 Jul 28 '21 at 11:23

3 Answers3

9

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.

jlbofh
  • 433
  • 5
  • 7
  • 3
    Unfortunately this didn't solved the problem for me (I didn't down voted your answer). BUT you guided me on the right path to the solution, and thank you for that! In my case, I'm doing a React-Native app, and I'm using the `zo0r/react-native-push-notification` package and I followed the doc which makes you configuring the `AndroidManifest.xml` file for both FCM and GCM. Remove the GCM part solved the issue. Again, Thank you ! (I'm up-voting your answer so that it will be 0 again :p – ZedTuX Nov 05 '18 at 08:27
5

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.

Yogitha Bellare
  • 109
  • 1
  • 5
0

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);
        }
    }
Jimmy3Sticks
  • 172
  • 1
  • 9