Skip to content

Instantly share code, notes, and snippets.

@bruceherve
Forked from nidhi-canopas/push_notification.go
Created January 19, 2023 08:24
Show Gist options
  • Save bruceherve/44586bb62b24ad3ea7601c86068f9bf3 to your computer and use it in GitHub Desktop.
Save bruceherve/44586bb62b24ad3ea7601c86068f9bf3 to your computer and use it in GitHub Desktop.
func SendPushNotification(deviceTokens []string) error {
decodedKey, err := getDecodedFireBaseKey()
if err != nil {
return err
}
opts := []option.ClientOption{option.WithCredentialsJSON(decodedKey)}
app, err := firebase.NewApp(context.Background(), nil, opts...)
if err != nil {
log.Debug("Error in initializing firebase : %s", err)
return err
}
fcmClient, err := app.Messaging(context.Background())
if err != nil {
return err
}
response, err := fcmClient.SendMulticast(context.Background(), &messaging.MulticastMessage{
Notification: &messaging.Notification{
Title: "Congratulations!!",
Body: "You have just implement push notification",
},
Tokens: deviceTokens,
})
if err != nil {
return err
}
log.Debug("Response success count : ", response.SuccessCount)
log.Debug("Response failure count : ", response.FailureCount)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment