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
Post a Comment