23.4 C
London
Thursday, May 9, 2024

Setup your Push Notification Server utilizing Firebase | by Dev D | Could, 2024


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

Prerequisite :

Import your Java challenge into Eclipse and add the dependencies of Firebase admin SDK to your Venture.

Initialise Firebase App :

Add Firebase Admin dependencies in pom.xml

<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<model>9.2.0</model> <!-- Replace model as wanted -->
</dependency>

Add Firebase Configuration to your Venture :

@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 chargeable for sending messages utilizing FirebaseMessaging.

@Service
public class FCMService {

non-public ultimate 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 Gadget.

Should you have no idea get FCM Token for Android units comply with this hyperlink

public class FCMRequestDTO {

non-public String title;
non-public String message;
non-public 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 {

non-public ultimate FCMService fcmService;

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

@PostMapping("/ship")
public ResponseEntity<String> 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 Internet App and name Api from Postman like

POST : localhost:8000/api/fcm/ship

Request Physique:

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

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

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

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here