Skip to content

Adds sendEach based snippets for FCM. #1104

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 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,36 @@ public void sendAll() throws FirebaseMessagingException {
// [END send_all]
}

public void sendEach() throws FirebaseMessagingException {
String registrationToken = "YOUR_REGISTRATION_TOKEN";

// [START send_each]
// Create a list containing up to 500 messages.
List<Message> messages = Arrays.asList(
Message.builder()
.setNotification(Notification.builder()
.setTitle("Price drop")
.setBody("5% off all electronics")
.build())
.setToken(registrationToken)
.build(),
// ...
Message.builder()
.setNotification(Notification.builder()
.setTitle("Price drop")
.setBody("2% off all books")
.build())
.setTopic("readers-club")
.build()
);

BatchResponse response = FirebaseMessaging.getInstance().sendEach(messages);
// See the BatchResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " messages were sent successfully");
// [END send_each]
}

public void sendMulticast() throws FirebaseMessagingException {
// [START send_multicast]
// Create a list containing up to 500 registration tokens.
Expand Down Expand Up @@ -201,6 +231,36 @@ public void sendMulticastAndHandleErrors() throws FirebaseMessagingException {
// [END send_multicast_error]
}

public void sendEachForMulticastAndHandleErrors() throws FirebaseMessagingException {
// [START send_each_for_multicast_error]
// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n"
);

MulticastMessage message = MulticastMessage.builder()
.putData("score", "850")
.putData("time", "2:45")
.addAllTokens(registrationTokens)
.build();
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
if (response.getFailureCount() > 0) {
List<SendResponse> responses = response.getResponses();
List<String> failedTokens = new ArrayList<>();
for (int i = 0; i < responses.size(); i++) {
if (!responses.get(i).isSuccessful()) {
// The order of responses corresponds to the order of the registration tokens.
failedTokens.add(registrationTokens.get(i));
}
}

System.out.println("List of tokens that caused failures: " + failedTokens);
}
// [END send_each_for_multicast_error]
}

public Message androidMessage() {
// [START android_message]
Message message = Message.builder()
Expand Down