1

We are using the Firebase function for push notifications. We've directly trigger and scheduled trigger push notifications. For that, we are using the Firebase function with typescript. Since users are getting increase we are using Topic based push notifications. create and subscribe happening from the mobile end like the following:

If the user is already subscribed, I unsubscribe that user and subscribe to the topic that the user has newly chosen.

if (name !== undefined) {
  const nameLower = name.toLowerCase()
  if (countryName) {
    isSubscribe ? await messaging().subscribeToTopic(nameLower) : await messaging().unsubscribeFromTopic(nameLower)
 }
}

if (platform) {
 isSubscribe ? await messaging().subscribeToTopic(platform) : await messaging().unsubscribeFromTopic(platform)
}

mIds?.forEach(async element => {
 if (element) {
   isSubscribe ? await messaging().subscribeToTopic(element) : await messaging().unsubscribeFromTopic(element)
 }
});

arrIds?.forEach(async element => {
 const createTopic = `${element?.tId}-${element?.rId}`
 if (createTopic) {
   isSubscribe ? await messaging().subscribeToTopic(createTopic) : await messaging().unsubscribeFromTopic(createTopic)
 }
});

Above is doing subscribe and unsubscribe when onboarding. Once the user is onboarded, To show foreground notification and to catch click on notification I am having following NotificationCenter.ts

useEffect(() => {
  // Showing foreground notification
  const unsubscribe = messaging().onMessage((remoteMessage) => {
  if (remoteMessage.notification) {
    showMessage({
      message: remoteMessage.notification.title,
      description: remoteMessage.notification.body,
      type: "success",
      duration: 3000,
      statusBarHeight: StatusBar.currentHeight,
      onPress: () => {
        remoteMessage?.data?.Id && details(remoteMessage?.data?.Id)
      },
     })
    }
  })

  return () => {
    unsubscribe()
  }
}, [])


useEffect(() => {
 // setting iOS batch count to 0
 return notifee.onForegroundEvent(({ type, detail }) => {
  switch (type) {
    case EventType.PRESS:
      notifee.setBadgeCount(0)
      detail.notification?.data && details(detail.notification?.data?.Id as string)
      break
   }
 })
}, [])

useEffect(() => {
//Initial notification
 messaging()
  .getInitialNotification()
  .then((remoteMessage) => {
    if (remoteMessage) {
      remoteMessage.data.Id && details(remoteMessage.data.Id)
    }
  })
}, [])

useEffect(() => {
 //Notification open
 messaging().onNotificationOpenedApp((remoteMessage) => {
  remoteMessage.data.Id && details(remoteMessage.data.Id)
 })
}, [])

const details = (Id: string) => {
 // detail function
 client.notificationDetails(true)
 navigate("detailsScreen", { Id })
}

Then we're using onWrite firebase function event to send notifications(system handle notification) to react native app like the following: (we are using up to 5 conditions. Not using more than 5 conditional expression(OR) due to firebase restriction)

//Making condition dynamically
if (global.length > 0) {
  condition = `'${data?.IdString}' in topics`;
} else {
  condition = fireb.docs.reduce(
    (cnd, data, i) =>
      `${cnd}${i > 0 ? ' || ' : ''}'${details?.Id[0]}-${
        data.data().refId
      }' in topics`,
    '',
  );
  // Example topic 1: "xxxxxxx-xxxxx-<firebase doc id>' in topics || 'xxxxxxx-xxxxx-<firebase doc id>' in topics";
  // Example topic 1: "xxxxxxx-xxxxx-<firebase doc id>' in topics
}

const message = {
 notification: {
  title: `${name} - ${title}`,
  body: details?.heading?.body,
 },
 apns: {
  payload: {
    aps: {
      sound: 'default',
      badge: 1,
    },
  },
 },
 data: {refId: `${data?.Id}`},
 condition: condition, //Dynamically made condition
};
//send topic based push notification
const response = await admin.messaging().send(message);

In the dev and stage environment, we never had any duplicate notifications yet. (there is a small number of users and topics. So not many subscribers). But in prod, we are publishing content to the created topics, and many users are subscribed(1k-10k). Not often but from time to time we are getting duplicated notifications. Today our app got 10 notifications but two notifications get duplicated. One got four duplicates and another got two duplicated. I am checking all logs in the cloud function but it has triggered once and there are no logs that indicate it over-triggered(more than once). I have done many testing but couldn't figure it out. If anyone sees a bug in the above code that make notification duplication, please help us with the answer!!!

Mobile message SDK - "@react-native-firebase/messaging": "^16.4.0"

Node.js Admin SDK - "@google-cloud/tasks": "^3.1.0", "firebase-admin": "^10.0.2", "firebase-functions": "^3.18.0",

Is it a bug in Firebase Admin SDK?

Fahry Mohammed
  • 599
  • 5
  • 18
  • Check if the `onMessage` callback is being called for the same notification payload multiple times on the client-side. The Firebase SDK might be delivering the same notification payload multiple times, causing the duplication. You can log the received `remoteMessage.notification` object in the `onMessage` callback to see if it's the same for the duplicate notifications. However, if you exhaust all other possibilities and suspect a bug, you can reach out to Firebase support for further assistance. or File a bug in this [link](https://issuetracker.google.com/). – Chanpols May 25 '23 at 19:45
  • @Chanpols Thank you for your comment. But `onMessage` only trigger while the app on the foreground. This problem not only occurs while in the foreground but also the background. what I am doing in `onMessage` is, if the app is in the foreground(app open), I am showing an in-app notification(like an app alert) since system notification won't show up when the app is in the foreground. Also `onMessage` triggers when the app is in the foreground and it can't be able to duplicate the system notifications while the phone in the background, right? – Fahry Mohammed May 26 '23 at 04:18
  • You're correct that the `onMessage` callback is triggered when the app is in the foreground and doesn't directly affect the system notifications received when the app is in the background. I apologize for the confusion. Review the logic in your Firebase Cloud Function that sends the notifications, Check if you have any other code or services in your app that might be triggering additional notifications, Examine the logs and metrics in the Firebase console for the delivery status of the notifications – Chanpols May 26 '23 at 23:13
  • Verify that the server-side code responsible for sending notifications is functioning as expected, Ensure that the device tokens or topics used for subscribing to notifications are correctly managed and updated, Test the notification flow thoroughly on different devices and operating system versions, and If possible, try using a different testing environment or staging environment to isolate and reproduce the issue. – Chanpols May 26 '23 at 23:15

0 Answers0