4

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.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
Usama Tahir
  • 1,707
  • 3
  • 15
  • 30
  • Does the problem persist if it's not executed in a for loop? What is the log output? Are success or failure messages logged for devices that do not receive a notification? – Paul Lammertsma Oct 31 '18 at 16:21
  • 1
    notifications are working fine. I will observe them for 2,3 days and see if they are working correctly or not. I will update you after 2 days. – Usama Tahir Oct 31 '18 at 19:57

0 Answers0