Skip to main content

Firebase Cloud Messaging — Part 1 — Send Messages From the Backend

Firebase Cloud Messaging is one of the easiest and reliable ways of sending messages/notifications from the backend to the front-end. So, I would like to share my experience with a cloud messaging service. The best part is, we all know that firebase has been providing a set of great services for free of charge. FCM is one of them. You could use this service for multiple scenarios in multiple ways. I am going to follow the Admin SDK option in our example which is the popular way of doing it. Also, in part — 01 of this topic, I will be focusing on sending the message from the back end, and in part — 02 of this topic I will show you how you can handle the received messages in the front-end.

Admin SDK supports almost all the languages and technologies such as Java, ASP.NET Core, Node JS, etc. in this example I will be using the ASP.NET Core to demonstrate the back-end side of it. Feel free to use any languages that you prefer.

In order to get started with the FCM admin SDK, you need to create a Firebase project. To create one go to the firebase console then click on create project -> give it a name and go through the terms and conditions-> hit on create. Once you create the project go to the project settings.

Then open the “service account” and hit on “Generate new private key” button.


It will download a JSON with the credentials of the project. As I mentioned earlier there are a lot of ways you can achieve this. Since we are using Admin SDK we do not need to worry about the authentication much. Admin SDK will handle that on behalf of you. You just need to parse the credentials (You have downloaded above) as an environment variable or a JSON string.


Once you create our app instance, you can send push notifications using the implementations in the Amin SDK. But still, you need to worry about one thing. It is nothing but the destination you are targeting. There can be a lot of devices registered in the application/projects. So, how do we send the message to the device/group? Well, the answer is the firebase token. When you register the device in the firebase project, your device can receive a unique token and will be refreshed in the event of expiry. Here, the front-end must make sure that the server is having the token up to date. I will be demonstrating that when we investigate the front-end side in the next part. This solves the problem which is sending the message to a particular device. Then how do we send it to a group of devices? For that, you can create a group in the project and add the devices to that. So, while you are sending the message, you can point to the group.

I believe that gives some context around the concept. Now it’s time to jump into the practical implementation. Before we start, keep that in mind, I do not recommend having the credentials and tokens in the code. Also, I do not recommend messing up with the core logic and concrete implementations. Anyhow to demonstrate the concept, I will be doing them in a simple console application. So, feel free to apply your own way of implementation based on the nature of your application.

First of all, install the admin SDK to your project from the NuGet package manager.


Now, we need to get the firebase messaging instance. To do this I am creating two read-only variables in the implementation class one is to have the Firebase Messaging instance and the other one is to store the credentials JSON as JSON string (This is only for demonstration purpose. You should not do this the credentials have to come from a secured wallet/ environment variables). Here, keep in mind you should not assign a new instance of the firebase app while you already have one running in the current instance of your service. Therefore, I am checking whether my app is null before creating a new instance of my APP (line #21, #22). I am searching the app by “[Default]” because I do not have any name for that. If you have the app name in the project settings, you should use that instead of the default. After you got the instance of the app you can create the messaging instance (line #26) and assign it to your variable (line #16).


Now we have the instance to start sending the message. But before we move forward let me explain the structure of the message.


In an FCM message, you will be having four main properties which are mentioned above in the screenshot. Here the token is going to be a device-specific token (you can send a message to the group as well, for that, you need to give the event name instead of the token), Message is going to be a <string, string> dictionary where you can add key-value pairs as much as you want, Title is the title of the message and body is going to be a small description of the message.

After you create a message you can simply pass that to the SendAsync method of your current Firebase Messaging Instance (line #40). It will return you the message-id in the event of successful delivery. After that you are good to send messages from your back end.



I believe this example gives you a good understanding to get started with FCM. Thank you so much for your time in reading this. Stay tuned for part-02 subscribing messages and displaying them to the users.

What are you waiting for? Go ahead and get started implementing FCM in your applications!

Resources

Git Repo : https://github.com/vithushanms/FCM-demo.git

Firebase documentation : Firebase Cloud Messaging (google.com)

LinkedIn: msvithushan

Instagram: ivithushan

Comments

Popular posts from this blog

Batch processing in modern Java applications with Spring Batch.

In the world of software development, Batch processing has been one of the challenging areas to implement in the early stages. But these days there are plenty of solutions available out of the box in the frameworks and platforms to do batch processing. In this article, I will share my experience with one such tool, Spring Batch. This will enable you to do batch processing by configuring this with your existing Spring Boot applications. Before we jump into the Spring Batch let me brief you about batch processing. the name might make you fear a lot about it in case if you are new to this. It’s nothing but processing the transactions (data) as small chunks or groups without any manual interaction from the user of the application. You may ask why you want to do that. The answer is because it has a lot of benefits in terms of performance and the efficiency of the application as you deal with a large dataset.  Now let’s understand how it’s going to work and how we are going to implement this

What inspired spaceX to perform a historical achievement

As we know on Saturday, May 30th at 3.22 p.m EDT SpaceX has successfully launched the crew dragon’s demo-2 with NASA astronauts Bob Behnken and Doug Hurley on top of Falcon-9 . For American soil, it’s a 9 years gap from the STS shuttle. Also, this is the first privately-owned space vehicle to bring humans to Low Earth Orbit. The biggest question is “How the SpaceX managed to do this?”. If you want to know the answer you have to know the journey of SpaceX from the beginning. In 2002, SpaceX was formed by Elon Musk and Tom Mueller (Present CTO of SpaceX) with the vision of colonizing humans in the Red Planet. In the early 2000s, SpaceX was a joke for the space exploration community. Everyone knows that transporting humans to Mars is a big deal. Still, the biggest achievement in mars is NASA’s Curiosity Mars Rover. Elon Musk was very clear about this at the time when SpaceX was founded. Also, his fortune was not enough to go for launch at that time. But they were ready to face the challen

FaaS in Action with AWS + Serverless Framework for Java Developers

Serverless is becoming popular and Function as a Service is one of the trends in the serverless world. So being familiar with this technology is a really good addition to the profile as a developer. But from the very beginning of serverless, the community was preferring to use javaScript as the programming language. The reason for that is Serverless was initially made for less compute-intensive and more I/O intensive services where the JavaScript is performing well. So, that leads JavaScript to hold nearly 90% of the serverless world. But moving forward the way we use serverless and utilize computational power has changed. So there is nothing wrong for a Java developer to try out the FaaS and build comprehensive services. Therefore, I will be giving a practical example for developing an API in Java and deploying it in the AWS cloud. There are a few prerequisites to follow along Basic knowledge developing an API Basic idea about AW