0
votes

I want to send fcm push notifications to android and ios app. I am using this code to send

let message = {
    registration_ids: firebaseId, // this is the array of tokens
        collapse_key: 'something',
        data: {
          type: data.type,
          title: data.title,
          body : data.body,
          notificationId: something
        },
  }; 
  fcm.send(message, (err, response) => {
      if (err) {
        console.log(err);
        resolve(false);
      } else {
        console.log("Notification Android Sent Successfully");
        console.log(response);
      }
});

Now what I want if some notifications are failed then I want to send them SMS. but I got a response like this form the fcm server in case of success or failure.

Notification Android Sent Successfully {"multicast_id":7535512435227354255,"success":2,"failure":1,"canonical_ids":0,"results":[{"message_id":"0:1610622370449056%d0bd483c86f03759"},{"message_id":"0:1610622370449058%d0bd483c86f03759"},{"error":"InvalidRegistration"}]}

now how will I know which device did not get the notification as we can see there 1 failure from 3 device so i can send SMS to that device

1
Hey Vipul. Did you get anywhere with this? I tried to help with an answer below on how to parse the results. Did you see that? Did it make sense? If my answer was useful, click the upvote button (▲) to the left of it. If it answered your question, click the checkmark (✓) to accept it. That way others know that you've been (sufficiently) helped. Also see What should I do when someone answers my question? - Frank van Puffelen
Brother actually i was logged out for some days that why there is no reply, but this you answer did not helped me i know that third was failed but how to get that token for which it failed right no i have to match response erro index to firebaseids array to figure out - Vipul Chaursiya

1 Answers

0
votes

The results in the response you get contains the result for each token, in the same order in which you specified them,

So in your example, the message was successfully sent to the first two tokens, while the third token was unknown. You'll want to remove that last token from your database to prevent trying to send messages to it in the future.