Skip to content

Commit c2ab431

Browse files
authored
Added snippets for new multicast APIs (#265)
* Added snippets for new multicast APIs * Format fixed * Added full stop
1 parent 4822459 commit c2ab431

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

snippets/messaging.go

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func sendAll(ctx context.Context, client *messaging.Client) {
110110
// This registration token comes from the client FCM SDKs.
111111
registrationToken := "YOUR_REGISTRATION_TOKEN"
112112

113-
// [START send_a_batch_golang]
113+
// [START send_all]
114114
// Create a list containing up to 100 messages.
115115
messages := []*messaging.Message{
116116
{
@@ -136,8 +136,72 @@ func sendAll(ctx context.Context, client *messaging.Client) {
136136

137137
// See the BatchResponse reference documentation
138138
// for the contents of response.
139-
fmt.Println(br.SuccessCount, " messages were sent successfully")
140-
// [END send_a_batch_golang]
139+
fmt.Printf("%d messages were sent successfully\n", br.SuccessCount)
140+
// [END send_all]
141+
}
142+
143+
func sendMulticast(ctx context.Context, client *messaging.Client) {
144+
// [START send_multicast]
145+
// Create a list containing up to 100 registration tokens.
146+
// This registration tokens come from the client FCM SDKs.
147+
registrationTokens := []string{
148+
"YOUR_REGISTRATION_TOKEN_1",
149+
// ...
150+
"YOUR_REGISTRATION_TOKEN_n",
151+
}
152+
message := &messaging.MulticastMessage{
153+
Data: map[string]string{
154+
"score": "850",
155+
"time": "2:45",
156+
},
157+
Tokens: registrationTokens,
158+
}
159+
160+
br, err := client.SendMulticast(context.Background(), message)
161+
if err != nil {
162+
log.Fatalln(err)
163+
}
164+
165+
// See the BatchResponse reference documentation
166+
// for the contents of response.
167+
fmt.Printf("%d messages were sent successfully\n", br.SuccessCount)
168+
// [END send_multicast]
169+
}
170+
171+
func sendMulticastAndHandleErrors(ctx context.Context, client *messaging.Client) {
172+
// [START send_multicast_error]
173+
// Create a list containing up to 100 registration tokens.
174+
// This registration tokens come from the client FCM SDKs.
175+
registrationTokens := []string{
176+
"YOUR_REGISTRATION_TOKEN_1",
177+
// ...
178+
"YOUR_REGISTRATION_TOKEN_n",
179+
}
180+
message := &messaging.MulticastMessage{
181+
Data: map[string]string{
182+
"score": "850",
183+
"time": "2:45",
184+
},
185+
Tokens: registrationTokens,
186+
}
187+
188+
br, err := client.SendMulticast(context.Background(), message)
189+
if err != nil {
190+
log.Fatalln(err)
191+
}
192+
193+
if br.FailureCount > 0 {
194+
var failedTokens []string
195+
for idx, resp := range br.Responses {
196+
if !resp.Success {
197+
// The order of responses corresponds to the order of the registration tokens.
198+
failedTokens = append(failedTokens, registrationTokens[idx])
199+
}
200+
}
201+
202+
fmt.Printf("List of tokens that caused failures: %v\n", failedTokens)
203+
}
204+
// [END send_multicast_error]
141205
}
142206

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

0 commit comments

Comments
 (0)