---
# System prepended metadata

title: Firebase Cloud Messaging - Spring Boot

---

# Firebase Cloud Messaging - Spring Boot

[TOC]

## High-level Overview
![](https://i.imgur.com/zYBLmAj.png)

## Set Up
* Include Firebase Admin dependency in Spring Boot.

```
<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin -->
<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>9.0.0</version>
    <scope>runtime</scope>
</dependency>

```
    
* Need to authorize requests from our spring backend application to FCM. Refer to https://firebase.google.com/docs/cloud-messaging/auth-server.

## Sending Messages
Use Cases: 
* Send message to specific user with his/her token
* Send message to users who subscribe to a certain topic
* Send to everyone
Code Examples can be found at: https://firebase.google.com/docs/cloud-messaging/send-message

Note: Frontend needs to implement handling of push notification from FCM.

## Receive Upstream Messages
Use Cases:
* Receive message from client app.
Code Examples can be found at: https://firebase.google.com/docs/cloud-messaging/receive-upstream

## Generating private key for service accounts
On the firebase console, click the gear icon next to Project Overview on the top left corner > Service Accounts > Generate new private key. 

This will generate a new private key, authenticating the bearer to some of firebase's features, including FCM for push notifications. 

The key is in json and should be included in the Spring Boot project application directory. 

## Spring Boot Project 
![](https://i.imgur.com/uPRuh4N.png)
Attached is a picture showing the project directory of the Spring Boot application, with the json key file at the root. 

In application.properties, define a variable, which stores the path to the key file. 
![](https://i.imgur.com/jwYOHeS.png)

There is a config class, called FirebaseConfig, whose purpose is similar to UploadConfig for Minio. In this class is a function with the @PostConstruct annotation, which is called after creation of this service bean, for initializing our firebase application. 

## FCM Service

__Code for sending notfication to target__ 
![](https://i.imgur.com/KOatGDt.png)
Takes a string as parameter called target, which is the user token to which, the notification is going to be sent. 
__Code for sending notification to topic__
![](https://i.imgur.com/r6psg18.png)
Takes a string as parameter: topic, which is the target topic, for the notification to be sent to. 

__Code for subscribing a user to topic__
![](https://i.imgur.com/2xObGQC.png)
Takes the user token and topic. Subscribes the user to the given topic. Note that the __subscribeToTopic__ function takes a list as input. As such, we can subscribe a group of users all in 1 batch by calling the function only once. 

The payload data used here is hardcoded. Ideally, there should be a model class for this, which can be defined easily. The payload data is then sent back to the frontend. How the data is used and displayed, depends on the frontend's handling of the notification.

For testing purposes, there is a controller class, containing endpoints which exposes these functions. Refer to 
https://documenter.getpostman.com/view/17882811/UzQvtkfb

Feel free to modify/extend these functions as appropriate. 
