fcm push notifications not received on all devices when send in a for loop

Viewed 669

I am using fcm to send push notifications to mobile devices including both ios and android. I have a table in firestore that has device ids of the registered users. I loop through that table and send push notifications to mobile devices. I am using following code for push notification.

const sendNotification = (deviceId, userId) => {
  return new Promise((resolve, reject) => {
    let message = {
      notification: {
        title: 'TITLE',
        body: `notification is sent to ${userId}`
      }
    };
    let options = {
      contentAvailable: true,
      priority: "high",
      timeToLive: 60 * 60 * 24
    };
    firebase.messaging().sendToDevice(deviceId, message, options)
      .then(function (response) {
        resolve({
          message: `Successfully sent message`
        })
        console.log(`notification sent to ${userId}`);
      })
      .catch(function (error) {
        reject({
          message: "There is an issue sending push notification"
        })
        console.log('Error sending message:', error);
      });

  });
};

the problem is notification is sent successfully to all devices but is not received by all devices. Sometimes it is delivered on device A and when I rerun the code, push notification is not delivered to that device. Sometimes push notifications are received on all devices and sometimes none of the devices gets any push notification. I am calling sendNotification in a for loop that is basically iterating the documents present in a table and each document contains user id of the user and device id of that user's mobile device.

0 Answers
Related