diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7221e8f --- /dev/null +++ b/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.6 + + + com.springhow.examples + spring-boot-fcm-demo + 0.0.1-SNAPSHOT + spring-boot-fcm-demo + Send FCM notifications from spring boot + + + 17 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.google.firebase + firebase-admin + 9.1.1 + + + org.projectlombok + lombok + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/springhow/examples/springbootfcmdemo/SpringBootFcmDemoApplication.java b/src/main/java/com/springhow/examples/springbootfcmdemo/SpringBootFcmDemoApplication.java new file mode 100644 index 0000000..23f419d --- /dev/null +++ b/src/main/java/com/springhow/examples/springbootfcmdemo/SpringBootFcmDemoApplication.java @@ -0,0 +1,33 @@ +package com.springhow.examples.springbootfcmdemo; + +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 SpringBootFcmDemoApplication { + + @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, "my-app"); + return FirebaseMessaging.getInstance(app); + } + + public static void main(String[] args) { + SpringApplication.run(SpringBootFcmDemoApplication.class, args); + } + +} diff --git a/src/main/java/com/springhow/examples/springbootfcmdemo/controllers/TestController.java b/src/main/java/com/springhow/examples/springbootfcmdemo/controllers/TestController.java new file mode 100644 index 0000000..f9d0b7c --- /dev/null +++ b/src/main/java/com/springhow/examples/springbootfcmdemo/controllers/TestController.java @@ -0,0 +1,27 @@ +package com.springhow.examples.springbootfcmdemo.controllers; + +import com.google.firebase.messaging.FirebaseMessagingException; +import com.springhow.examples.springbootfcmdemo.pojo.Note; +import com.springhow.examples.springbootfcmdemo.service.FirebaseMessagingService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class TestController { + + private final FirebaseMessagingService firebaseService; + + public TestController(FirebaseMessagingService firebaseService) { + this.firebaseService = firebaseService; + } + + @RequestMapping("/send-notification") + @ResponseBody + public String sendNotification(@RequestBody Note note, + @RequestParam String topic) throws FirebaseMessagingException { + return firebaseService.sendNotification(note, topic); + } +} diff --git a/src/main/java/com/springhow/examples/springbootfcmdemo/pojo/Note.java b/src/main/java/com/springhow/examples/springbootfcmdemo/pojo/Note.java new file mode 100644 index 0000000..6b9663a --- /dev/null +++ b/src/main/java/com/springhow/examples/springbootfcmdemo/pojo/Note.java @@ -0,0 +1,13 @@ +package com.springhow.examples.springbootfcmdemo.pojo; + +import lombok.Data; + +import java.util.Map; + +@Data +public class Note { + private String subject; + private String content; + private Map data; + private String image; +} diff --git a/src/main/java/com/springhow/examples/springbootfcmdemo/service/FirebaseMessagingService.java b/src/main/java/com/springhow/examples/springbootfcmdemo/service/FirebaseMessagingService.java new file mode 100644 index 0000000..205f050 --- /dev/null +++ b/src/main/java/com/springhow/examples/springbootfcmdemo/service/FirebaseMessagingService.java @@ -0,0 +1,36 @@ +package com.springhow.examples.springbootfcmdemo.service; + +import com.google.firebase.messaging.*; +import com.springhow.examples.springbootfcmdemo.pojo.Note; +import org.springframework.stereotype.Service; + +@Service +public class FirebaseMessagingService { + + private final FirebaseMessaging firebaseMessaging; + + public FirebaseMessagingService(FirebaseMessaging firebaseMessaging) { + this.firebaseMessaging = firebaseMessaging; + } + + + public String sendNotification(Note note, String topic) throws FirebaseMessagingException { + + Notification notification = Notification + .builder() + .setTitle(note.getSubject()) + .setBody(note.getContent()) + .setImage(note.getImage()) + .build(); + + Message message = Message + .builder() + .setTopic(topic) + .setNotification(notification) + .putAllData(note.getData()) + .build(); + + return firebaseMessaging.send(message); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/src/main/resources/firebase-service-account.json b/src/main/resources/firebase-service-account.json new file mode 100644 index 0000000..6b20552 --- /dev/null +++ b/src/main/resources/firebase-service-account.json @@ -0,0 +1,12 @@ +{ + "type": "service_account", + "project_id": "spring-demo", + "private_key_id": "d159fd09beREDACTED9599d1ead79acdf23125", + "private_key": "<<<>>>", + "client_email": "firebase-adminsdk-himxs@spring-demo.iam.gserviceaccount.com", + "client_id": "1078469REDACTED4481171", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-himxs%40spring-demo.iam.gserviceaccount.com" +}