Created
March 9, 2022 18:20
-
-
Save nidhi-canopas/3dfa1d91e2d61547644ac97fdcb60897 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
@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
It's not the server key from Cloud Messaging API.
It's the service account key, in the form of base64 encoded.
Reference link to generate firebase service account key