I have a springboot application which I’m hosting on my own home server. I have sql database setup on the same.
And for front end I’m planning to use android for initial testing phase then shift it to flutter.
I was wonder how do I send notifications from my spring boot to my front end application. I have seen a few articles on how to send it through fire base but I was wondering if there’s another way of achieving the same without using an external service.
I have setup my server running Ubuntu on on 3 pc which loadbalances my app and want to use one of them to send push notifications.
Solution 1: Nilesh Senta
Please follow the below steps
- Install Dependency (Gradle/ Maven)
Gradle
implementation 'com.google.firebase:firebase-admin:8.1.0'
Maven
<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>8.1.0</version> </dependency>
- Add firebase-service-account.json file
../src/main/resources/firebase-service-account.json
- Open MainApplication java class
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
@SpringBootApplication
public class BackendApplication {
public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
}
@Bean
FirebaseMessaging firebaseMessaging() throws IOException {
GoogleCredentials googleCredentials = GoogleCredentials
.fromStream(new ClassPathResource("firebase-service-account.json").getInputStream());
FirebaseOptions firebaseOptions = FirebaseOptions
.builder()
.setCredentials(googleCredentials)
.build();
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "YOUR APP NAME");
return FirebaseMessaging.getInstance(app);
}
}
- Create Service
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import org.springframework.stereotype.Service;
@Service
public class FirebaseMessagingService {
private final FirebaseMessaging firebaseMessaging;
public FirebaseMessagingService(FirebaseMessaging firebaseMessaging) {
this.firebaseMessaging = firebaseMessaging;
}
public void sendNotification(String title, String body, String token) throws FirebaseMessagingException {
Notification notification = Notification
.builder()
.setTitle(title)
.setBody(body)
.build();
Message message = Message
.builder()
.setToken(token)
.setNotification(notification)
// .putAllData(note.getData())
.build();
firebaseMessaging.send(message);
}
}
- Usage of service inside controller
@Autowired
private FirebaseMessagingService firebaseService;
public void sendPushMessage(){
firebaseService.sendNotification("Notification title", "Notification Text", "Receiver device token");
}