From 77c81303a8452ba2d2d63ac17d6458bb66f7f510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=9A=A9=EC=88=98?= Date: Tue, 24 Sep 2024 11:41:17 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20FCM=20Service=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/worlabel/global/config/FCMConfig.java | 2 -- .../worlabel/global/service/FCMService.java | 29 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/com/worlabel/global/service/FCMService.java diff --git a/backend/src/main/java/com/worlabel/global/config/FCMConfig.java b/backend/src/main/java/com/worlabel/global/config/FCMConfig.java index fe3d47e..d99066d 100644 --- a/backend/src/main/java/com/worlabel/global/config/FCMConfig.java +++ b/backend/src/main/java/com/worlabel/global/config/FCMConfig.java @@ -40,12 +40,10 @@ public class FCMConfig { FirebaseApp firebaseApp = FirebaseApp.getApps().isEmpty() ? FirebaseApp.initializeApp(options) : FirebaseApp.getInstance(); - log.debug("firebase 불러오기 성공"); // 설정된 FirebaseApp 연결된 FirebaseMessaging 인스턴스를 반환 return FirebaseMessaging.getInstance(firebaseApp); } catch (IOException e) { throw new CustomException(ErrorCode.SERVER_ERROR, "Firebase Key 불러오기 실패 "); } } - } diff --git a/backend/src/main/java/com/worlabel/global/service/FCMService.java b/backend/src/main/java/com/worlabel/global/service/FCMService.java new file mode 100644 index 0000000..ccac82b --- /dev/null +++ b/backend/src/main/java/com/worlabel/global/service/FCMService.java @@ -0,0 +1,29 @@ +package com.worlabel.global.service; + +import com.google.firebase.messaging.FirebaseMessaging; +import com.google.firebase.messaging.Message; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class FCMService { + + private final FirebaseMessaging firebaseMessaging; + + private void sendNotification(String targetToken, String title, String body){ + Message message = Message.builder() + .setToken(targetToken) + .putData("title",title) + .putData("body",body) + .build(); + try { + log.debug("FCM 알림 전송 {}", message); + firebaseMessaging.sendAsync(message); + } catch (Exception e) { + log.error("FCM 전송 오류 ", e); + } + } +}