Send push notifications from Spring Boot server

This blog is about to send push notifications with spring boot and FCM. I will tell you how to send push notifications from the server.

What is FCM?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.

How FCM work

  1. Registration: When application gets installed it registers with FCM.
  2. Token: FCM returns token to app.
  3. Receive token:  Frontend send token information to backend and backend save token in database.
  4. Trigger FCM service layer : Trigger the REST API from backend to this particular service layer in the FCM.
  5. Trigger Notification: FCM will intern trigger the push notification to mobile apps.

Start from the FCM integration.

We have to generate our own Firebase SDK admin key.It’s a JSON file with your Firebase project credentials.

Go to Firebase Console Project Settings to Service Account and Generate new private key and save file. We’ll use it in the next step.

Spring Boot Application

First of all, add a field in I added a new key/value pair containing a file path.

#put the path of FireBase-adminsdk file


Now we’ll need to add maven dependencies of Firebase in the pom.xml file.

Now we have to initialize our Firebase application. This is the time to use our app.firebase-configuration-file. I used @Value annotation to inject the path value to the String field.

Send Notification

I created a PushNotificationRequest class to add some mandatory fields.

Next step is to prepare for our FCMService class.

In this class we completed  Android configuration and Apple Push Notification service (APNs) configuration.

Sending push notifications with payload data.

 We can test it right now by calling the proper method. We are using spring scheduler to send push notifications. 

For scheduling, I used @Scheduled annotation with cron job(0 0/2 * 1/1 * ?) to send push notifications every  2 min. Remember to annotate your Application class with @EnableScheduling.

It is configured properly and working fine.

Look at this screenshot with received notifications interval of 2 min.




Leave a Reply