Skip to content

Commit f1869ee

Browse files
authored
Adds new snippets for sendEach methods. (#395)
1 parent cccc005 commit f1869ee

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

messaging/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,35 @@ function sendMulticast() {
180180
// [END fcm_send_multicast]
181181
}
182182

183+
function sendEachForMulticast() {
184+
// [START fcm_send_each_for_multicast]
185+
// These registration tokens come from the client FCM SDKs.
186+
const registrationTokens = [
187+
'YOUR_REGISTRATION_TOKEN_1',
188+
// …
189+
'YOUR_REGISTRATION_TOKEN_N',
190+
];
191+
192+
const message = {
193+
data: {score: '850', time: '2:45'},
194+
tokens: registrationTokens,
195+
};
196+
197+
getMessaging().sendEachForMulticast(message)
198+
.then((response) => {
199+
if (response.failureCount > 0) {
200+
const failedTokens = [];
201+
response.responses.forEach((resp, idx) => {
202+
if (!resp.success) {
203+
failedTokens.push(registrationTokens[idx]);
204+
}
205+
});
206+
console.log('List of tokens that caused failures: ' + failedTokens);
207+
}
208+
});
209+
// [END fcm_send_each_for_multicast]
210+
}
211+
183212
function sendAll() {
184213
const registrationToken = '...';
185214

@@ -202,6 +231,28 @@ function sendAll() {
202231
// [END fcm_send_all]
203232
}
204233

234+
function sendEach() {
235+
const registrationToken = '...';
236+
237+
// [START fcm_send_each]
238+
// Create a list containing up to 500 messages.
239+
const messages = [];
240+
messages.push({
241+
notification: { title: 'Price drop', body: '5% off all electronics' },
242+
token: registrationToken,
243+
});
244+
messages.push({
245+
notification: { title: 'Price drop', body: '2% off all books' },
246+
topic: 'readers-club',
247+
});
248+
249+
getMessaging().sendEach(messages)
250+
.then((response) => {
251+
console.log(response.successCount + ' messages were sent successfully');
252+
});
253+
// [END fcm_send_each]
254+
}
255+
205256
function notificationMessage() {
206257
// [START fcm_notification_message]
207258
const topicName = 'industry-tech';

0 commit comments

Comments
 (0)