Golang — Send Push notifications to apps using FCM

Nidhi D
Canopas
Published in
3 min readMar 15, 2022

--

Golang: Send Push notifications to apps using FCM

“Craving a pizza? 🍕 💛 Order now and get $5 off !!”

“Hurry up!! You get a 50% discount on a purchase of minimum Rs.1000/- 🎉 .”

We become more excited on seeing our screen blinking with alerts like above.
Ever think about how it reaches your mobile? Push notification is behind the scene.

When we heard the word push notification for the first time, we must have a thought that how can our mobile be recognized by someone so that they can notify us.

In this blog, we will explore how push notifications are sent using Golang and Firebase.

Keylines

  1. Basic Introduction to firebase and a device token
  2. Configure Firebase auth key
  3. Initialize firebase client
  4. Send Push notifications to a single device
  5. Send Push notifications to multiple devices

1. Basic Introduction to firebase and a device token

The device token is the main contestant, responsible for pushing notifications. It’s not a fancy word, it’s simply an id that is unique for each device. When we install any application, our device’s unique token is being stored on their server, using which they can notify us.

Firebase is the tool, used for pushing notifications to apps/mobiles/tablets.
It provides a setting to configure the cloud messaging (FCM) service, which is used for push notifications.

Note: The firebase config files used at the app side (google_services.json file for android and GoogleService-Info.plist for iOS) and service_account key must be created for the same project from the firebase console.

In this blog, we will explore how push notifications are sent using Golang and Firebase.

Here, I’m assuming that you’re familiar with firebase and configured your project in the firebase console. If not done already, do it as instructed.

Let’s jump to implementation.

2. Configure Firebase auth key

Required packages:

  1. firebase.google.com/go
  2. firebase.google.com/go/messaging

It’s always better to use sensitive stuff in an encrypted format. To serve that purpose I’ve stored the service account key in format base64 encoded as an environment variable.

The sample service account key,

service account key

However, you also can use it in raw format, if security is not concerned.

Here, FIREBASE_AUTH_KEY contains a base64 encoded service account key.

First, let’s decode the service account key.

The above method will return decoded service account key that will be used in the next step.

Retrieve decoded key and Initialize the firebase app, using the below code.

3. Initialize firebase client

Create fcmClient for messaging(pushing notifications).

Finally, let’s write a code to send a notification.

4. Send Push notifications to a single device

The above code will send a push notification to a single device.

5. Send Push notifications to multiple devices

Consider a scenario if you want to notify multiple devices. It will be almost the same as we send a push notification to a single device, just need to replace fcmClient’s Send() method with SendMulticast().

Yay !! You have implemented push notifications in your Golang project 🎊.
The response variable will contain the success count and failure count of sent notifications.

Find the full code at FCM with Golang. Invoke SendPushNotification() and boom !! Check a notification received in the application.

Meanwhile, you can also check to debug logs, as it has SuccessCount and failureCount. It will help to verify that notifications are pushed to how many tokens. In case of finding failureCount > 0, you must verify whether you’re giving a valid device token or not.

Keep notifying !!

Thanks for your support!

If you like what you read, be sure to 👏 👏👏 it below — as a writer it means the world!

Follow Canopas Software to get updates on interesting articles!

--

--

Web developer@canopas | Gravitated towards Web | Eager to assist