@@ -149,6 +149,36 @@ public void sendAll() throws FirebaseMessagingException {
149
149
// [END send_all]
150
150
}
151
151
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
+
152
182
public void sendMulticast () throws FirebaseMessagingException {
153
183
// [START send_multicast]
154
184
// Create a list containing up to 500 registration tokens.
@@ -201,6 +231,36 @@ public void sendMulticastAndHandleErrors() throws FirebaseMessagingException {
201
231
// [END send_multicast_error]
202
232
}
203
233
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
+
204
264
public Message androidMessage () {
205
265
// [START android_message]
206
266
Message message = Message .builder ()
0 commit comments