@@ -140,6 +140,40 @@ func sendAll(ctx context.Context, client *messaging.Client) {
140
140
// [END send_all]
141
141
}
142
142
143
+ func sendEach (ctx context.Context , client * messaging.Client ) {
144
+ // This registration token comes from the client FCM SDKs.
145
+ registrationToken := "YOUR_REGISTRATION_TOKEN"
146
+
147
+ // [START send_each]
148
+ // Create a list containing up to 500 messages.
149
+ messages := []* messaging.Message {
150
+ {
151
+ Notification : & messaging.Notification {
152
+ Title : "Price drop" ,
153
+ Body : "5% off all electronics" ,
154
+ },
155
+ Token : registrationToken ,
156
+ },
157
+ {
158
+ Notification : & messaging.Notification {
159
+ Title : "Price drop" ,
160
+ Body : "2% off all books" ,
161
+ },
162
+ Topic : "readers-club" ,
163
+ },
164
+ }
165
+
166
+ br , err := client .SendEach (context .Background (), messages )
167
+ if err != nil {
168
+ log .Fatalln (err )
169
+ }
170
+
171
+ // See the BatchResponse reference documentation
172
+ // for the contents of response.
173
+ fmt .Printf ("%d messages were sent successfully\n " , br .SuccessCount )
174
+ // [END send_each]
175
+ }
176
+
143
177
func sendMulticast (ctx context.Context , client * messaging.Client ) {
144
178
// [START send_multicast]
145
179
// Create a list containing up to 500 registration tokens.
@@ -189,6 +223,40 @@ func sendMulticastAndHandleErrors(ctx context.Context, client *messaging.Client)
189
223
if err != nil {
190
224
log .Fatalln (err )
191
225
}
226
+ if br .FailureCount > 0 {
227
+ var failedTokens []string
228
+ for idx , resp := range br .Responses {
229
+ if ! resp .Success {
230
+ // The order of responses corresponds to the order of the registration tokens.
231
+ failedTokens = append (failedTokens , registrationTokens [idx ])
232
+ }
233
+ }
234
+ fmt .Printf ("List of tokens that caused failures: %v\n " , failedTokens )
235
+ }
236
+ // [END send_multicast_error]
237
+ }
238
+
239
+ func sendEachForMulticastAndHandleErrors (ctx context.Context , client * messaging.Client ) {
240
+ // [START send_each_for_multicast_error]
241
+ // Create a list containing up to 500 registration tokens.
242
+ // This registration tokens come from the client FCM SDKs.
243
+ registrationTokens := []string {
244
+ "YOUR_REGISTRATION_TOKEN_1" ,
245
+ // ...
246
+ "YOUR_REGISTRATION_TOKEN_n" ,
247
+ }
248
+ message := & messaging.MulticastMessage {
249
+ Data : map [string ]string {
250
+ "score" : "850" ,
251
+ "time" : "2:45" ,
252
+ },
253
+ Tokens : registrationTokens ,
254
+ }
255
+
256
+ br , err := client .SendEachForMulticast (context .Background (), message )
257
+ if err != nil {
258
+ log .Fatalln (err )
259
+ }
192
260
193
261
if br .FailureCount > 0 {
194
262
var failedTokens []string
@@ -201,7 +269,7 @@ func sendMulticastAndHandleErrors(ctx context.Context, client *messaging.Client)
201
269
202
270
fmt .Printf ("List of tokens that caused failures: %v\n " , failedTokens )
203
271
}
204
- // [END send_multicast_error ]
272
+ // [END send_each_for_multicast_error ]
205
273
}
206
274
207
275
func sendDryRun (ctx context.Context , client * messaging.Client ) {
0 commit comments