Setup your Push Notification Server using Firebase
In today’s digital age, push notifications play a crucial role in engaging users and driving user retention for mobile applications…
In today’s digital age, push notifications play a crucial role in engaging users and driving user retention for mobile applications. Firebase Cloud Messaging (FCM) is a powerful solution provided by Google for sending push notifications to Android, iOS, and web platforms. In this article, we will explore how to set up a push notification server using Firebase, allowing you to send real-time notifications to your mobile app users efficiently.
In this story, I will help you set up a Push Notification Server using Java Springboot and send Notifications to registered devices.
Prerequisite :
Install Java 17
Install Eclipse
Initialize a SpringBoot app from https://start.spring.io/ .
To get FCM Token for Android App check this link .
Import your Java project into Eclipse and add the dependencies of Firebase admin SDK to your Project.
Initialise Firebase App :
Add Firebase Admin dependencies in pom.xml
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>9.2.0</version> <!-- Update version as needed -->
</dependency>
Add Firebase Configuration to your Project :
@Configuration
public class FirebaseConfig {
@Bean
public FirebaseApp firebaseApp() throws IOException {
FileInputStream serviceAccount = new FileInputStream("./devicecontrol.json");
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
return FirebaseApp.initializeApp(options);
}
}
Service
Create a FCMService service class in SpringBoot App which is responsible for sending messages using FirebaseMessaging.
@Service
public class FCMService {
private final FirebaseMessaging firebaseMessaging;
@Autowired
public FCMService(FirebaseApp firebaseApp) {
this.firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp);
}
public String sendMessage(FCMRequestDTO requestDTO) throws FirebaseMessagingException {
Message message = Message.builder()
.putData("title", requestDTO.getTitle())
.putData("body", requestDTO.getMessage())
.setToken(requestDTO.getDeviceToken())
.build();
String response = firebaseMessaging.send(message);
return response;
}
}
Request Model :
Create a Request Model that contains Metadata for Notification with FCM Token of Mobile Device.
If you do not know how to get FCM Token for Android devices follow this link
public class FCMRequestDTO {
private String title;
private String message;
private String deviceToken;
public FCMRequestDTO() {
}
// Getter and Setter
}
Controller
Right a controller that exposes the endpoint to send notifications when you call this API :
@RestController
@RequestMapping("/api/fcm")
public class FCMController {
private final FCMService fcmService;
@Autowired
public FCMController(FCMService fcmService) {
this.fcmService = fcmService;
}
@PostMapping("/send")
public ResponseEntity<String> sendMessage(@RequestBody FCMRequestDTO requestDTO) {
try {
return ResponseEntity.ok(fcmService.sendMessage(requestDTO));
} catch (FirebaseMessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ResponseEntity.ok("FCM message sent successfully.");
}
}
Now Run this Web App and call Api from Postman like
POST : localhost:8000/api/fcm/send
Request Body:
{
"title":"Title",
"message":"Message",
"deviceToken":"Enter Android Device FCM Token"
}
Once you call the above API you will get a Notification to your device.
Clap and Follow me on Linkedin for more awesome blogs.
Comment to me for the Full code.