Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Created March 9, 2022 18:20
Show Gist options
  • Save nidhi-canopas/3dfa1d91e2d61547644ac97fdcb60897 to your computer and use it in GitHub Desktop.
Save nidhi-canopas/3dfa1d91e2d61547644ac97fdcb60897 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
}
@Bit4z
Copy link

Bit4z commented Nov 11, 2022

@nidhi-canopas I am trying to integrate this code in my project and getting following error.

error := SenderId mismatch

Following is the function where I have read server account key file which is generated from firebase messaging service account section.

func getDecodedFireBaseKey() ([]byte, error) {

decodedKey, err := ioutil.ReadFile("server_account_key.json")
if err != nil {
	log.Fatal(err)
}
return decodedKey, nil

}

Please help to resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment