-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2689a52
Showing
8 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.6</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.springhow.examples</groupId> | ||
<artifactId>spring-boot-fcm-demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>spring-boot-fcm-demo</name> | ||
<description>Send FCM notifications from spring boot</description> | ||
|
||
<properties> | ||
<java.version>17</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.firebase</groupId> | ||
<artifactId>firebase-admin</artifactId> | ||
<version>9.1.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/springhow/examples/springbootfcmdemo/SpringBootFcmDemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/springhow/examples/springbootfcmdemo/controllers/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/springhow/examples/springbootfcmdemo/pojo/Note.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> data; | ||
private String image; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/springhow/examples/springbootfcmdemo/service/FirebaseMessagingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"type": "service_account", | ||
"project_id": "spring-demo", | ||
"private_key_id": "d159fd09beREDACTED9599d1ead79acdf23125", | ||
"private_key": "<<<<REDACTED>>>>", | ||
"client_email": "[email protected]", | ||
"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" | ||
} |