Skip to content

Commit 4ca8578

Browse files
authored
Adds new sendEach snippets. (#702)
1 parent 613e052 commit 4ca8578

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

snippets/messaging.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,40 @@ func sendAll(ctx context.Context, client *messaging.Client) {
140140
// [END send_all]
141141
}
142142

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+
143177
func sendMulticast(ctx context.Context, client *messaging.Client) {
144178
// [START send_multicast]
145179
// Create a list containing up to 500 registration tokens.
@@ -189,6 +223,40 @@ func sendMulticastAndHandleErrors(ctx context.Context, client *messaging.Client)
189223
if err != nil {
190224
log.Fatalln(err)
191225
}
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+
}
192260

193261
if br.FailureCount > 0 {
194262
var failedTokens []string
@@ -201,7 +269,7 @@ func sendMulticastAndHandleErrors(ctx context.Context, client *messaging.Client)
201269

202270
fmt.Printf("List of tokens that caused failures: %v\n", failedTokens)
203271
}
204-
// [END send_multicast_error]
272+
// [END send_each_for_multicast_error]
205273
}
206274

207275
func sendDryRun(ctx context.Context, client *messaging.Client) {

0 commit comments

Comments
 (0)