1.9 C
New York
Sunday, December 1, 2024

Setup your Push Notification Server utilizing Firebase | by Dev D


In at present’s digital age, push notifications play a vital function in participating customers and driving person retention for cell functions. Firebase Cloud Messaging (FCM) is a strong answer supplied by Google for sending push notifications to Android, iOS, and net platforms. On this article, we’ll discover how you can arrange a push notification server utilizing Firebase, permitting you to ship real-time notifications to your cell app customers effectively.

On this story, I’ll enable you arrange a Push Notification Server utilizing Java Springboot and ship Notifications to registered units.

Prerequisite :

Import your Java venture into Eclipse and add the dependencies of Firebase admin SDK to your Undertaking.

Initialise Firebase App :

Add Firebase Admin dependencies in pom.xml


com.google.firebase
firebase-admin
9.2.0

Add Firebase Configuration to your Undertaking :

@Configuration
public class FirebaseConfig {

@Bean
public FirebaseApp firebaseApp() throws IOException {
FileInputStream serviceAccount = new FileInputStream("./devicecontrol.json");

FirebaseOptions choices = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.construct();

return FirebaseApp.initializeApp(choices);
}
}

Service
Create a FCMService service class in SpringBoot App which is liable for sending messages utilizing FirebaseMessaging.

@Service
public class FCMService {

personal remaining 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("physique", requestDTO.getMessage())
.setToken(requestDTO.getDeviceToken())
.construct();
String response = firebaseMessaging.ship(message);
return response;
}
}

Request Mannequin :
Create a Request Mannequin that incorporates Metadata for Notification with FCM Token of Cell System.

When you have no idea how you can get FCM Token for Android units observe this hyperlink

public class FCMRequestDTO {

personal String title;
personal String message;
personal String deviceToken;

public FCMRequestDTO() {

}

// Getter and Setter

}

Controller

Proper a controller that exposes the endpoint to ship notifications once you name this API :

@RestController
@RequestMapping("/api/fcm")
public class FCMController {

personal remaining FCMService fcmService;

@Autowired
public FCMController(FCMService fcmService) {
this.fcmService = fcmService;
}

@PostMapping("/ship")
public ResponseEntity sendMessage(@RequestBody FCMRequestDTO requestDTO) {

strive {

return ResponseEntity.okay(fcmService.sendMessage(requestDTO));
} catch (FirebaseMessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ResponseEntity.okay("FCM message despatched efficiently.");
}
}

Now Run this Net App and name Api from Postman like

POST : localhost:8000/api/fcm/ship

Request Physique:

{
"title":"Title",
"message":"Message",
"deviceToken":"Enter Android System FCM Token"
}

When you name the above API you’ll get a Notification to your machine.

Clap and Observe me on Linkedin for extra superior blogs.
Remark to me for the Full code.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles