Duplicate fcm push notification in react native android backgroundhandler

Viewed 1006

I developed react native application with rnfirebase and notifee for sending the push notification. foreground is working properly, message is displayed only once. but the background notification is displaying twice like one is from messaging().setBackgroundMessageHandler and another one is android's default push notification. First message is from default push notification and next one is from firebase messaging. So how do I remove android's default push notification. I'm also checked that first default notification is not using the firebase messaging and notifee. It's comes from outside of react native like android's native push notification

See this image

1 Answers

The notifications that you are seeing are most likely one from firebase and another from Notifee. In my project I was handling the notifications that were coming from firebase via firebase.messaging().onMessage and inside this listener I was showing a local notification using Notifee so that the notification shows in the foreground.

async showNotificationInForeground(message: FirebaseMessagingTypes.RemoteMessage) {
    const { messageId, notification, data } = message
    const channelId = await Notifee.createChannel({
      id: messageId,
      name: 'Pressable Channel',
      importance: AndroidImportance.HIGH,
    })

    await Notifee.displayNotification({
      title: notification?.title || '',
      body: notification?.body || '',
      data,
      android: {
        channelId,
        importance: AndroidImportance.HIGH,
        pressAction: {
          id: messageId,
        },
        smallIcon: 'ic_stat_name',
        localOnly: true,
      },
    })
  }

However what was happening was that I was calling this showNotificationInForeground method to show the local Notifee notification on both firebase's background and messaging listeners ie: firebase.messaging().onMessage and firebase.messaging().setBackgroundMessageHandler

So what I ended up doing was only calling the showNotificationInForeground method in onMessage listener and not in setBackgroundMessageHandler, which resulted in showing the local notification in the foreground, and the firebase notification in the background.

If this is not the case for you, you are most likely registering an extra notification receiver inside your AndroidManifest.xml file which is causing the duplication

Related