Skip to content

Adds new snippets for sendEach methods. #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions messaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '...';

Expand All @@ -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';
Expand Down