As you probably noticed, I familiarize myself with the subject of Firebase Cloud Messaging push notifications recently.
This time I would like to share with you my push notifications server-side application. The app is made with the Spring Boot framework.
The provided example covers most common notifications sending use cases such as: sending push notification to a topic, directly to the users’ device, or sending messages with additional data payload.
This post is not a step-by-step tutorial. I’ll focus on the most important parts. A fully working example is available on GitHub.
If you don’t have a client-side application to receive push notifications yet, you should consider the use of the Ionic app I described earlier. Have fun!
Let’s start with the Firebase integration.
First of all, you have to generate your own Firebase SDK admin key. Basically, it’s a JSON file with your Firebase project credentials. You’ll need it for server-side authorization (more information).
Login in to your Firebase console. Go to Project settings -> Service accounts and then click Generate new private key button.
Generate and save the file. We’ll use it in the next steps.
Spring Boot application
Put generated Firebase Admin SDK JSON file inside your Spring Boot project files (if you don’t have one you can generate it here).
In my case, I created a google folder inside src/main/resources and then copied the generated file there (push-notifications-example-firebase-adminsdk.json).
Then, in my application.properties, I added a new key/value pair containing a file path.
Now, we’ll need Firebase dependencies, so let’s add some. I used Maven as a dependency manager.
In my pom.xml dependencies I put an additional dependency:
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.
The initialize() method is being called on application start-up thanks to @PostConstruct annotation.
Sending push notifications
The next step is to prepare our push notifications service class.
But first of all, I separated the following use cases:
- send push notification to the topic using sample data (defined in application.properties)
- send scheduled sample push notification to the topic (every minute)
- send a push notification with additional data payload (user-defined key/value data object, look at the docs for more information)
- send push notification without an additional data payload
- send push notification to a specific user
Sending push notification without data payload example from FCMService class:
If you really want to test it, you can try to run the application right now and call the proper methods.
In my case, for clarity purposes, I added another layer – PushNotificationService which will be used directly by PushNotificationController in the further steps.
Let’s take a look at PushNotificationService:
As you see, we have a @Value annotation again over here.
Why? Because we have a bunch of default values to pass to our methods.
So instead of injecting a single value from properties to a particular String variable, I decided to use Map type for simplicity. It still uses the same @Value annotation.
Again, we store the defaults in application.properties. So the app.notifications.defaults key looks like this:
Moreover, you can see we’re using the FCMService directly, passing notifications requests objects to it.
For scheduling, I used @Scheduled annotation with an initial delay of 1min. (initialDelay parameter) and 1min. sending interval (fixedDelay parameter). Remember to annotate your Application class with @EnableScheduling.
Let’s interact with our services and build a controller.
For presentation purposes I exposed my service methods as REST API endpoints in order to send custom, direct messages (to the topic or specific subscriber) with/without data payload or trigger the default notification sending.
Test REST endpoints with cURL
Let’s test our endpoints. I used cURL for this task but you can use your favorite HTTP requests tools or write a small client app for this.
- GET /notification – Trigger sample notification with default values sending
- POST /notification/topic – Send a message to a specific topic
- POST /notification/token – Send a message to a specific device (with the token)
- POST /notification/data – Send a message to a specific topic with additional payload data.
Please note: in this case I took defaults from application.properties as sample payload. In your application, you should probably use third-party API data or persistent data from your database.
If succeeded you should receive the following JSON response with code 200:
Well… If configured properly it just works. I used my Ionic app and Android phone as a client.
Take a look at this screenshot with received notifications:
In this case, the client-side app doesn’t handle additional payload data.
Are you curious about the yellow color of notifications? I set it in NotificationParameter enum along with sound option value (which is default in my case).
See the reference for more additional fields description.
In this post, I covered basic Firebase Cloud Messaging integration with the Spring Boot application. If you’re looking for server-side push notification implementation I think this is the way to go.
As I mentioned before, the fully working example is available on GitHub. If you’re looking for a basic client-side mobile application please take a look at my previous Ionic push notifications app post.
Please note that the provided example was tested with an Android application. Although I provided some basic Apple Push Notification Service (APNS) configuration you have to deal with potential issues when developing an iOS application. If you managed to do it please let me know.