Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
raja-anbazhagan committed May 6, 2023
0 parents commit 2689a52
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
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/
46 changes: 46 additions & 0 deletions pom.xml
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>
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);
}

}
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);
}
}
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;
}
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);
}

}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 12 additions & 0 deletions src/main/resources/firebase-service-account.json
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"
}

0 comments on commit 2689a52

Please sign in to comment.