Skip to content
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