diff --git a/snippets/messaging.go b/snippets/messaging.go index 2b073dcb..7e950d31 100644 --- a/snippets/messaging.go +++ b/snippets/messaging.go @@ -272,6 +272,42 @@ func sendEachForMulticastAndHandleErrors(ctx context.Context, client *messaging. // [END send_each_for_multicast_error] } +func sendEachForMulticastAndHandleErrors(ctx context.Context, client *messaging.Client) { + // [START send_each_for_multicast_error] + // Create a list containing up to 500 registration tokens. + // This registration tokens come from the client FCM SDKs. + registrationTokens := []string{ + "YOUR_REGISTRATION_TOKEN_1", + // ... + "YOUR_REGISTRATION_TOKEN_n", + } + message := &messaging.MulticastMessage{ + Data: map[string]string{ + "score": "850", + "time": "2:45", + }, + Tokens: registrationTokens, + } + + br, err := client.SendEachForMulticast(context.Background(), message) + if err != nil { + log.Fatalln(err) + } + + if br.FailureCount > 0 { + var failedTokens []string + for idx, resp := range br.Responses { + if !resp.Success { + // The order of responses corresponds to the order of the registration tokens. + failedTokens = append(failedTokens, registrationTokens[idx]) + } + } + + fmt.Printf("List of tokens that caused failures: %v\n", failedTokens) + } + // [END send_each_for_multicast_error] +} + func sendDryRun(ctx context.Context, client *messaging.Client) { message := &messaging.Message{ Data: map[string]string{