Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Created March 9, 2022 18:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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
}
@f-ezzahra
Copy link

how can I get the deviceToken?

@nidhi-canopas
Copy link
Author

nidhi-canopas commented Jun 27, 2022

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

@f-ezzahra
Copy link

f-ezzahra commented Jun 27, 2022

@nidhi-canopas but I'm using golang, is there any package in golang that uses the same thing?

@nidhi-canopas
Copy link
Author

nidhi-canopas commented Jun 29, 2022

@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.

@Plaoo
Copy link

Plaoo commented Sep 16, 2022

@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)
	}

@kevinkjs
Copy link

@nidhi-canopas is this example use background notification? or foreground?

@Bit4z
Copy link

Bit4z commented Nov 11, 2022

@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
Copy link
Author

@nidhi-canopas is this example use background notification? or foreground?

Background

@nidhi-canopas
Copy link
Author

@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.

Reference link to generate firebase service account key

@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