1admin.messaging().sendToDevice(tokens, payload).then((response) => {
2 // For each message check if there was an error.
3 const tokensToRemove = [];
4 response.results.forEach((result, index) => {
5 const error = result.error;
6 if (error) {
7 console.error('Failure sending notification to', tokens[index], error);
8 // Cleanup the tokens who are not registered anymore.
9 if (error.code === 'messaging/invalid-registration-token' ||
10 error.code === 'messaging/registration-token-not-registered') {
11 // TODO: remove the token from your registry/database
12 }
13 }
14 });
15});
16