7

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.

paulsm4
123k23 gold badges175 silver badges245 bronze badges
asked Jun 2, 2022 at 3:23
5
  • 1
    The only way your home server can connect to your handset is if a) the handset's Wifi was on, it was on the same LAN as your server, and the server knew the handset's (probably dynamic) IP address. Or if b) the handset were connected directly to the server PC (e.g. Bluetooth or USB cable). Both alternatives are a bit "limiting" (to put it charitably). Your best bet is almost certainly to use a "cloud" solution like Firebase. Nevertheless, you might be interested in this article: medium.com/flutter-community/… Commented Jun 2, 2022 at 3:32
  • For Sending Push Notification Your Can Use FCM (Firebase Cloud Messaging) its free and easy to implement on both web and mobile end. Commented Jun 2, 2022 at 5:42
  • sample available springhow.com/spring-boot-firebase-push-notification Commented Jun 2, 2022 at 5:56
  • @Priyesh Jakhmola - you explicitly asked "I was wondering if there’s another way of achieving the same without using an external service.". Yesterday, I suggested that, for many good reasons, a "cloud" solution (like Firebase) was probably the best way to go. It sounds like you're coming around to agree. Commented Jun 3, 2022 at 3:16
  • 1
    @paulsm4 yeah after going over the article you had attached and doing some research it looks like using firebase is one of the best ways to implement this. Though it's always nice to know there are other options as well. Thanks for sharing that article with me. Commented Jun 3, 2022 at 9:54

1 Answer 1

3

Please follow the below steps

  1. 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>
  1. Add firebase-service-account.json file

../src/main/resources/firebase-service-account.json

  1. 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);
 }
}
  1. 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);
// For Send to multiple devices use Multicast Message Builder
 MulticastMessage message = MulticastMessage
 .builder()
 .addAllTokens(<List Of Tokens>)
 .setNotification(notification)
// .putAllData(note.getData())
 .build();
 firebaseMessaging.send(message);
 }
}
  1. Usage of service inside controller
 @Autowired
 private FirebaseMessagingService firebaseService;
 
 public void sendPushMessage(){
 firebaseService.sendNotification("Notification title", "Notification Text", "Receiver device token");
 } 
answered Jun 2, 2022 at 5:57
Sign up to request clarification or add additional context in comments.

6 Comments

okay this seems like a nice way to implement firebase directly in my springboot app, but i have one question how will i get the Receiver device token that you mentioned in the sendPushMessage() function?
This token come from android app when user install your app, firebase generate token for that device and you need to save this in your database. please check (pub.dev/packages/firebase_messaging/example)
What if we want to send a notification to a group of users? Like broadcasting a notification to a group of users?
@coder001 In this case you should use MulticastMesage class, check edited answer.
I have the problem, that I cannot autowire the FirebaseMessaging class because there are "No beans of 'FirebaseMessaging' type found" (according to InelliJ). How did you solve that issue? I use Kotlin btw if that makes any difference
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.