Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud messaging duplicate notifications

We are using Firebase cloud messaging. Sometimes when Android or iOS app is in sleep mode the cell phone receives same (duplicates) notifications messages. For device identifications the FIRInstanceID token is used. An external server on node.js is used for sending notifications to Firebase service. No duplications in our server log file are presented.

like image 798
Kisin Pavel Avatar asked Mar 18 '26 16:03

Kisin Pavel


2 Answers

in file firebase-messaging-sw.js

comment this line

// self.registration.showNotification(notificationTitle,notificationOptions)

and no duplicate fcm anymore

like image 185
bemygon Avatar answered Mar 20 '26 06:03

bemygon


If you are working with Firebase-sw.js then here is what is happening.

"Note this is not for admin SDK"

When you send "notification" key in the payload it triggers the default behaviour of the of the browser and it shows the notification. Then in your service provider you are showing the notification again:

self.registration.showNotification(notificationTitle,notificationOptions)

...You are not at fault because the firebase documentation did not mention any details about it.

Solution You have to remove the "notification" key from your payload and modify your firebase-sw.js file like this:

messaging.onBackgroundMessage(function(payload) {
  console.log('Received background message ', payload);

  const notificationTitle = payload.data.title;
  const notificationOptions = {
    body: payload.data.body
  };
  self.registration.showNotification(notificationTitle, notificationOptions);
});

and your payload should look like this:

{
    "data": {
        "title":"New Ride Request",
        "body":"A new ride has been requested",
        "sound" : "default"
    },
    "condition": "'topic1' in topics"
}  

Now your default notification will not trigger and your code level notification will be displayed only.

like image 30
Syed Avatar answered Mar 20 '26 06:03

Syed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!