-
-
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 | |
} |
how can I get the deviceToken?
@f-ezzahra You can get it from any of the clients like android, iOS or flutter. which you need to store in database, while doing sign-in/sign-up.
here is a reference you can go through, Get device tokens in android
@nidhi-canopas but I'm using golang, is there any package in golang that uses the same thing?
@f-ezzahra I don't think there's any way of doing it, as the device token is ultimately a unique id of a specific mobile/tablet/ipad. It's a complete client thing.
@nidhi-canopas but I'm using golang, is there any package in golang that uses the same thing?
client, err := app.Auth(ctx)
user, err := client.GetUserByEmail(context.Background(), email)
if err != nil {
log.Fatalf("error getting user by email %s: %v\n", email, err)
}
@nidhi-canopas is this example use background notification? or foreground?
@nidhi-canopas I am trying to integrate this code in my project and getting following error.
decode key error:= illegal base64 data at input byte 11
Following is the function where I have assigned server key in variable as I do not have it in the environment variables.
func getDecodedFireBaseKey() ([]byte, error) { fireBaseAuthKey := "MyServerKey" decodedKey, err := base64.StdEncoding.DecodeString(fireBaseAuthKey) if err != nil { return nil, err } return decodedKey, nil }
I have taken the server key from "Cloud Messaging API" section on google cloud account.
Please help to resolve the issue.
@nidhi-canopas is this example use background notification? or foreground?
Background
@nidhi-canopas I am trying to integrate this code in my project and getting following error.
decode key error:= illegal base64 data at input byte 11
Following is the function where I have assigned server key in variable as I do not have it in the environment variables.
func getDecodedFireBaseKey() ([]byte, error) { fireBaseAuthKey := "MyServerKey" decodedKey, err := base64.StdEncoding.DecodeString(fireBaseAuthKey) if err != nil { return nil, err } return decodedKey, nil }
I have taken the server key from "Cloud Messaging API" section on google cloud account.Please help to resolve the issue.
It's not the server key from Cloud Messaging API.
It's the service account key, in the form of base64 encoded.
@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.
how can I get the deviceToken?