From bf9ea65d579005ee8dcffd981815c65ad68f5fad Mon Sep 17 00:00:00 2001 From: joefspiro <97258781+joefspiro@users.noreply.github.com> Date: Thu, 12 Jun 2025 16:20:20 -0400 Subject: [PATCH] Adds new snippets for sendEach methods. --- messaging/index.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/messaging/index.js b/messaging/index.js index fba4ee86..0e30b365 100644 --- a/messaging/index.js +++ b/messaging/index.js @@ -180,6 +180,35 @@ function sendMulticast() { // [END fcm_send_multicast] } +function sendEachForMulticast() { + // [START fcm_send_each_for_multicast] + // These registration tokens come from the client FCM SDKs. + const registrationTokens = [ + 'YOUR_REGISTRATION_TOKEN_1', + // … + 'YOUR_REGISTRATION_TOKEN_N', + ]; + + const message = { + data: {score: '850', time: '2:45'}, + tokens: registrationTokens, + }; + + getMessaging().sendEachForMulticast(message) + .then((response) => { + if (response.failureCount > 0) { + const failedTokens = []; + response.responses.forEach((resp, idx) => { + if (!resp.success) { + failedTokens.push(registrationTokens[idx]); + } + }); + console.log('List of tokens that caused failures: ' + failedTokens); + } + }); + // [END fcm_send_each_for_multicast] +} + function sendAll() { const registrationToken = '...'; @@ -202,6 +231,28 @@ function sendAll() { // [END fcm_send_all] } +function sendEach() { + const registrationToken = '...'; + + // [START fcm_send_each] + // Create a list containing up to 500 messages. + const messages = []; + messages.push({ + notification: { title: 'Price drop', body: '5% off all electronics' }, + token: registrationToken, + }); + messages.push({ + notification: { title: 'Price drop', body: '2% off all books' }, + topic: 'readers-club', + }); + + getMessaging().sendEach(messages) + .then((response) => { + console.log(response.successCount + ' messages were sent successfully'); + }); + // [END fcm_send_each] +} + function notificationMessage() { // [START fcm_notification_message] const topicName = 'industry-tech';