Firebase send push notification twice

20,792

Solution 1

It looks like you are using com.google.android.gms:play-services:9.0.0 (which includes play-services-gcm) and com.google.firebase:firebase-messaging:9.0.0

FCM from firebase-massaging automatically registers an Instance ID token (device ID) so if you have logic that registers for a token in your app it is likely that you are registering twice. This could account for you receiving multiple notifications. More generally though you should not use FCM and GCM in the same app for exactly this reason. So if you are going to use FCM you should remove GCM from your app.

Also, using play-services includes all the play-services-x APIs like play-services-gcm and play-services-drive etc. So always use the split libraries like play-services-x instead of just play-services.

Solution 2

For me it was a 3th party sdk that was using GCM while our app was using FCM. Registering with both services will give you two notifications.

(Also I was calling super.onMessageReceived(remoteMessage) in my FirebaseMessagingService causing a third notification to appear :p

Solution 3

I was having the same problem when application was in background or closed (and sending both notification and data). Caused by requesting old GCM permissions in AndroidManifest.xml

Take a look at https://stackoverflow.com/a/44435980/1533285

Solution 4

I had to remove

<receiver android:name="com.google.android.gms.gcm.GcmReceiver" 
android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> 
  <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
  <category android:name="${applicationId}" /> 
  </intent-filter> 
</receiver>

from AndroidManifest.xml

This worked for me, thanks

Solution 5

The accepted answer did not work for me. Below is what worked:

I used data-messages instead of display-messages so that even if the app is in foreground or background, the only single notification will appear.

Replace

{
      "to": "/topics/journal",
      "notification": {
        "title" : "title",
        "text": "data!",
        "icon": "ic_notification"
       }
}

With

{
  "to": "/topics/dev_journal",
   "data": {
       "text":"text",
       "title":"",
       "line1":"Journal",
       "line2":"刊物"
   }
} 

Reference : How to handle notification when app in background in firebase

Share:
20,792
Siavash Abdoli
Author by

Siavash Abdoli

Dreamer that using stairs to go up. I believe everyone must go their own way. Android Developer At Cafebazaar

Updated on July 15, 2022

Comments

  • Siavash Abdoli
    Siavash Abdoli almost 2 years

    I wrote a very simple android app to test firebase push notification and I get one notification twice.

    this is the manifest service:

    <service
            android:name="com.google.firebase.messaging.FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service
            android:name="com.google.firebase.iid.FirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
    

    this is the app gradle:

        compile 'com.google.android.gms:play-services:9.0.0'
        compile 'com.google.firebase:firebase-core:9.0.0'
        compile 'com.google.firebase:firebase-messaging:9.0.0'
    }
    apply plugin: 'com.google.gms.google-services'
    

    and here is the project level gradle:

    classpath 'com.google.gms:google-services:3.0.0'
    
  • Siavash Abdoli
    Siavash Abdoli over 7 years
    seems one 3th party sdk use GCM and i don't know wichone :D how to find that?
  • Tom Bevelander
    Tom Bevelander over 7 years
    Use logic. Most libraries don't need GCM. Big (payed) services with admin panels or with build in chat. Things like Parse, Intercom, payment services etc. could use it.
  • Tom Bevelander
    Tom Bevelander over 7 years
    Also: check this question: stackoverflow.com/questions/21645071/…
  • pratham kesarkar
    pratham kesarkar over 6 years
    For me it was AndroidJobs which is using GCM to schedule Notification to older devices.
  • Nitin
    Nitin about 6 years
    If the device is registered twice in a production app . Please suggest solution for it?
  • amitfr
    amitfr about 6 years
    You also need to make sure you clean your build to remove all GCM related classes This worked for me stackoverflow.com/questions/48785262/…
  • Martin Zvarík
    Martin Zvarík almost 6 years
    YES! The second example fires the onbackgroundmessage function.. more here: github.com/firebase/quickstart-js/issues/71
  • karora
    karora almost 5 years
    Thanks for that (Also...) - that was exactly why I was seeing two :-)