How to send voip notification from a SpringBoot Application to an Android device using FCM
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost. FCM gives us the ability to send VoIP notifications with High priority to the android device.
Note: To send a voip notification that will call the onMessageReceived method of the FirebaseMessagingService in your android application, you need to send a notification message with only the data part. https://firebase.google.com/docs/cloud-messaging/concept-options. Given this peculiarity, I will show how to setup the Firebase admin sdk for SpringBoot and how to construct the Notification Message.
Firebase Admin Sdk Setup
Add the dependency to your pom.xml file
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.12.0</version>
</dependency>
In the “Service accounts” tab of your firebase project, you will see a description of how to initialise the FirebaseApp for you admin sdk. It is important that the initialization is done before you proceed to make the FCM feature.
Generating and Sending Voip Notification Message
private Message generateAndroidVoipMessage(String token, Map<String, String> extraData) {
AndroidConfig androidConfig = AndroidConfig.*builder*().setPriority(AndroidConfig.Priority.*HIGH*).build();
return Message.*builder*()
.setAndroidConfig(androidConfig)
.setToken(token)
.putAllData(extraData)
.build();
}
The input parameter “token” refers to the FCM device token that allows you to send notifications to the device. The data is represented as a map of String keys and String values. Your data should contain details about the caller that initiated the call. The caller name, caller id, maybe the callId. This you will retrieve in the *onMessageReceived *method in the android app and proceed to display the voip notification. This method returns a com.google.firebase.messaging.Message object that we pass as the input parameter to our sendNotification method whose content is as follows.
private String sendNotification(Message message) throws Exception {
return FirebaseMessaging.*getInstance*().send(message);
}
The details are similar for other admin sdks.
