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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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) {
}
Please help to resolve the issue.