Firebase Cloud Messaging – Spring Server to Push Notification Example | Spring Boot

In the article Firebase Cloud Messaging – How to Subscribe TOPIC & Receive Messages | Android, we have created an Android App that can subscribe/unsubscribe specific TOPIC and receive Message Data, but we used Firebase Notification Console GUI to generate Notification. Today, we’re gonna look at way to create a Spring Boot Application Server that can make message and push notification to Android Client via Firebase.

Related Post: Firebase Cloud Messaging – XMPP Server example to receive Upstream Messages | Spring Integration

I. Project Overview

1. Architecture

spring-boot-fcm-push-notification-architecture

2. Topic HTTP POST

Send to a single topic JavaSampleApproach:

Send to devices subscribed to topics ABC or XYZ:

This is the response:

3. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.4.RELEASE
– Spring Boot: 2.0.0.M2

4. Structure

spring-boot-fcm-push-notification-structure

AndroidPushNotificationsService is the service that provides send() method (with ClientHttpRequestInterceptor and RestTemplate POST request) to Firebase FCM.
WebController is a REST Controller that creates Message Data Entity and uses Service above to push Notification.

II. Practice

1. Get Server Key

Go to Settings of your Firebase Project in Firebase Console to get Server Key.
spring-boot-fcm-push-notification-server-key

2. Create Spring Boot project

– Using Spring Tool Suite/Eclipse to create Spring Boot project.
– Add Dependencies to pom.xml file:

3. Create Push Notification Service

Remember to copy your Server Key at the first step to FIREBASE_SERVER_KEY.

4. Create Controller

5. Run & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App.
– Open Browser, enter URL:
http://localhost:8080/send
spring-boot-fcm-push-notification-result-web

Look back to the Android App Client that can subscribe/unsubscribe TOPIC and receive Message:
– Android App Client in background:
spring-boot-fcm-push-notification-result-android

– Android App Client in running foreground:
spring-boot-fcm-push-notification-result-logcat

III. Source Code

SpringBootFCMPushNotif


Related Posts


3 thoughts on “Firebase Cloud Messaging – Spring Server to Push Notification Example | Spring Boot”

Got Something To Say:

Your email address will not be published. Required fields are marked *

*