FCM Push Notification using Java

Posted By : Pankaj Garg | 29-Jul-2022

Firebase Cloud Messaging Push Notification from BackEnd using FCM Tokens.

 

Firebase console is a messaging a cross-platform messaging tool owned by google which provides its own APIs for the transfer of messages across the platform it uses File Transfer Protocol(FTP) and Simple Mail Transfer Protocol(SMTP) to send messages.
 

Firebase Cloud Messaging has two editions the first one is free of cost but provides few functionalities and the other is a paid version of Messaging. The firebase provides Analytics, App Indexing, Authentication, Dynamic Links, Cloud Messaging, Notification, Invites, Crash Reporting, Test Labs, and Remote Configuration. The free version has all of these functionalities excluding Test Labs and also The free plan limit is 100, and cannot be raised. The Flame and Blaze Plans have an initial limit of 10,000 simultaneous database connections. This is to prevent abuse and can be raised by contacting Firebase support with 24 hours' notice.

 

The Firebase not only provides the above-mentioned functionalities but a No-SQL Database also where the messages, fcm tokens can also be stored.

 

The Firebase sends notifications using two of the main things

1. The FCM token is unique to every device and has an expiration of 8 hours to 99 days.

2. The second method is to send the notification based on the topics where the user can subscribe or unsubscribe to get the notifications.

 

Firebase sends a notification to devices using their fcm token which it also created using its API which is called using FrontEnd language and stores them with corresponding to their deviceId and it can also update the fcm tokens.

 

The Firebase Cloud Messaging can be implemented to the backend using several languages like Python, Java, NodeJS, Go, and C#

 

We will be using Java as a BackEnd language to implement and send notifications to devices by taking fcmTokens from FrontEnd.

 

 

Let’s Implement it on Spring Boot now using Java Language.

 

Let’s add the dependency for firebase-admin first from firebase-admin.

 

And all the other dependencies like Spring Web for creating rest APIs and Lombok to reduce boilerplate code.

 

Then create a project on the firebase console add all the default setting for now.

 

You can create project from firebase-console.

 

 

 

 

 

Step 1:

 

Step 2: Give project a name

 

 

 

 

 

 

 

 

 

Step 3:

 

Step 4: Select default firebase account and click on create project it will take some time from 1 minute to 5 minutes.

 

Step 5: Now generate the private key from Setting < Project Setting <Service Account < Select Java and click on generate private key, now the private key generated is to be kept save and is never to be exposed.

 

Now copy the key and save it in the resource file of your project.

 

After saving the json file in the resource let’s create a Bean to initialize the Firebase Console App in Main class of java .

 

Now create all the packages and class required to create an End Point.

 

Controller -> class MyController

Service -> class MyService

Domains -> class Note

The note class will have several variables like subject , content , data , token.

 

Model Class:-

 


 


 


 


 


 

Service Class:-


 

Controller Class:-

You can now send notification by using tokens.

About Author

Author Image
Pankaj Garg

Pankaj Garg is an experienced backend developer, specializing in Java and skilled in using technologies like MySQL, MongoDB, Spring Boot, Hibernate, and Spring Security. He has contributed to multiple internal and client projects. He has experience in creating POC projects such as Micro Services and Message Broker using Spring Security. He has also contributed to the development of the Book Store Management System, which utilizes Rest APIs to communicate from the database to the frontend, providing information on book availability. Along with his passion for coding, he also enjoys playing chess and badminton.

Request for Proposal

Name is required

Comment is required

Sending message..