Reputation: 307
Can someone please tell me what i'm doing wrong here, the notification is said to be sent but i'm always getting a failure count, which means non of the device tokens actually gets the push notification. Here's my code
const payload = {
notification: {
title: `${main_title}`,
body: `${stripped_body}`,
},
data: {
type: "article",
appBar_title: appBar_title !== undefined ? appBar_title : "",
approved: approved !== undefined ? approved : "",
article_body: article_body !== undefined ? article_body : "",
body_image: body_image !== undefined ? body_image : "",
date_created: date_created !== undefined ? date_created : "",
featured_image: featured_image !== undefined ? featured_image : "",
gavels: gavels !== undefined ? gavels : "",
article_id: id !== undefined ? id : "",
likes: likes !== undefined ? likes : "",
main_title: main_title !== undefined ? main_title : "",
views: views !== undefined ? views : "",
broadcast: broadcast !== undefined ? broadcast : "",
},
};
await messaging
.sendToDevice(deviceToken, payload)
.then((response) => {
console.log("Successfully sent message to::", response.successCount);
})
.catch((error) => {
console.log("Error sending message:", error);
});
the deviceToken is an array of device token as such;
I'm totally lost for options on what else to do
Upvotes: 1
Views: 1934
Reputation: 599631
If you call FCM with many tokens it is quite common for sending to some of those tokens to fail for some reasons. In fact, you should treat some of those failures as an instruction from FCM to remove outdated tokens from your database.
A good example of how to do this, is in this code from the Cloud Functions example of sending notifications:
tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
const response = await admin.messaging().sendToDevice(tokens, payload);
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
functions.logger.error(
'Failure sending notification to',
tokens[index],
error
);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
This code processes the all tokens in the response, and removes the expired tokens from the database. If the app instance that such a token came from is still being used, it will have registered a new token at this point, so will receive the message through that token already.
Removing stale tokens like this ensures that your token registry remains of a size that is directly related to the number of active app instances, and you'll see a fairly constant number of expired/invalid/unregistered tokens over time. Failing to remove stale tokens will lead to a constant increase in the number of stale tokens, and a decrease in the message delivery rate over time.
Also see the Firebase documentation on managing FCM tokens.
Upvotes: 2