Skip to content

Commit

Permalink
Merge pull request #131 from 9uttery/feat/firebase-integration-#130
Browse files Browse the repository at this point in the history
[Configuration] Firebase 기본 설정
  • Loading branch information
mingeun0507 authored Feb 21, 2024
2 parents cf43eab + be03f09 commit e8cb068
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
run: echo ${{ secrets.APPLICATION_PROD_BASE64 }} | base64 --decode > ./src/main/resources/application-prod.yml
shell: bash

- name: make madii-app-firebase-adminsdk-uriyk-c04677456f.json
run: echo ${{ secrets.FIREBASE_KEY_BASE64 }} | base64 --decode > ./src/main/resources/madii-app-firebase-adminsdk-uriyk-c04677456f.json
shell: bash

- name: Gradle Caching
uses: actions/cache@v3
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ application-dev.yml
application-prod.yml
application-jwt.yml
application-secret.yml

### Secret files ###
**/main/resources/**/*.json
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
implementation "org.springframework.boot:spring-boot-starter-data-mongodb"

// Firebase
implementation 'com.google.firebase:firebase-admin:9.2.0'

// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.guttery.madii.common.config.firebase;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.guttery.madii.common.exception.CustomException;
import com.guttery.madii.common.exception.ErrorDetails;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.io.InputStream;

@Configuration
public class FirebaseConfig {
@Value("${firebase.config}")
private String firebaseKeyFilePath;
@Value("${firebase.project-id}")
private String firebaseProjectId;

@PostConstruct
public void initialize() {
try (
final InputStream serviceAccount = new FileInputStream(firebaseKeyFilePath)
) {
final FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setProjectId(firebaseProjectId)
.build();


if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
} catch (final Exception e) {
throw CustomException.of(ErrorDetails.FIREBASE_INTEGRATION_FAILED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public enum ErrorDetails {

FILE_UPLOAD_FAILED("F001", HttpStatus.INTERNAL_SERVER_ERROR.value(), "파일 업로드에 실패했습니다."),

FIREBASE_INTEGRATION_FAILED("FI001", HttpStatus.INTERNAL_SERVER_ERROR.value(), "Firebase 연동 중 오류가 발생했습니다. 다시 시도해 주세요."),

;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.guttery.madii.domain.notification.domain.service;

public interface NotificationClient {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.guttery.madii.domain.notification.infrastructure;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.guttery.madii.domain.notification.domain.service.NotificationClient;
import org.springframework.stereotype.Component;

@Component
public class FirebaseNotificationClient implements NotificationClient {
public String sendNotification(String content, String token) {
try {
final Message message = Message.builder()
.setToken(token)
.putData("title", content)
.build();

return FirebaseMessaging.getInstance().send(message);

} catch (FirebaseMessagingException e) {
return "Failed";
}
}
}

0 comments on commit e8cb068

Please sign in to comment.