Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac183c0

Browse files
Adds sendEach based snippets for FCM. (#1104)
* Adds sendEach based snippets for FCM. * Corrects getting the messaging instance.
1 parent 4f73761 commit ac183c0

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

‎src/test/java/com/google/firebase/snippets/FirebaseMessagingSnippets.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ public void sendAll() throws FirebaseMessagingException {
149149
// [END send_all]
150150
}
151151

152+
public void sendEach() throws FirebaseMessagingException {
153+
String registrationToken = "YOUR_REGISTRATION_TOKEN";
154+
155+
// [START send_each]
156+
// Create a list containing up to 500 messages.
157+
List<Message> messages = Arrays.asList(
158+
Message.builder()
159+
.setNotification(Notification.builder()
160+
.setTitle("Price drop")
161+
.setBody("5% off all electronics")
162+
.build())
163+
.setToken(registrationToken)
164+
.build(),
165+
// ...
166+
Message.builder()
167+
.setNotification(Notification.builder()
168+
.setTitle("Price drop")
169+
.setBody("2% off all books")
170+
.build())
171+
.setTopic("readers-club")
172+
.build()
173+
);
174+
175+
BatchResponse response = FirebaseMessaging.getInstance().sendEach(messages);
176+
// See the BatchResponse reference documentation
177+
// for the contents of response.
178+
System.out.println(response.getSuccessCount() + " messages were sent successfully");
179+
// [END send_each]
180+
}
181+
152182
public void sendMulticast() throws FirebaseMessagingException {
153183
// [START send_multicast]
154184
// Create a list containing up to 500 registration tokens.
@@ -201,6 +231,36 @@ public void sendMulticastAndHandleErrors() throws FirebaseMessagingException {
201231
// [END send_multicast_error]
202232
}
203233

234+
public void sendEachForMulticastAndHandleErrors() throws FirebaseMessagingException {
235+
// [START send_each_for_multicast_error]
236+
// These registration tokens come from the client FCM SDKs.
237+
List<String> registrationTokens = Arrays.asList(
238+
"YOUR_REGISTRATION_TOKEN_1",
239+
// ...
240+
"YOUR_REGISTRATION_TOKEN_n"
241+
);
242+
243+
MulticastMessage message = MulticastMessage.builder()
244+
.putData("score", "850")
245+
.putData("time", "2:45")
246+
.addAllTokens(registrationTokens)
247+
.build();
248+
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
249+
if (response.getFailureCount() > 0) {
250+
List<SendResponse> responses = response.getResponses();
251+
List<String> failedTokens = new ArrayList<>();
252+
for (int i = 0; i < responses.size(); i++) {
253+
if (!responses.get(i).isSuccessful()) {
254+
// The order of responses corresponds to the order of the registration tokens.
255+
failedTokens.add(registrationTokens.get(i));
256+
}
257+
}
258+
259+
System.out.println("List of tokens that caused failures: " + failedTokens);
260+
}
261+
// [END send_each_for_multicast_error]
262+
}
263+
204264
public Message androidMessage() {
205265
// [START android_message]
206266
Message message = Message.builder()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /