From 65dd526f1cff6d6e0f7dc50e784a0547ac7b7565 Mon Sep 17 00:00:00 2001 From: BK Date: Tue, 19 Dec 2023 21:24:49 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20FCM=20messaging=20-=20async=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BE/common/exception/ErrorCode.java | 3 ++ .../BE/controller/FCMMessageController.java | 40 +++++++++++++++++++ .../igoMoney/BE/service/FCMTokenService.java | 32 ++++++++++----- .../BE/service/NotificationService.java | 2 +- 4 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 src/main/java/igoMoney/BE/controller/FCMMessageController.java diff --git a/src/main/java/igoMoney/BE/common/exception/ErrorCode.java b/src/main/java/igoMoney/BE/common/exception/ErrorCode.java index 435566d..1aac105 100644 --- a/src/main/java/igoMoney/BE/common/exception/ErrorCode.java +++ b/src/main/java/igoMoney/BE/common/exception/ErrorCode.java @@ -27,6 +27,9 @@ public enum ErrorCode { AUTH_CODE_INVALID(HttpStatus.UNAUTHORIZED, "Authorization Code가 유효하지 않습니다."), FCM_TOKEN_INVALID(HttpStatus.UNAUTHORIZED, "FCM토큰이 null입니다."), + // FCM Messaging 예외 + FCM_MESSAGE_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "FCM 메세징 오류"), + // login 예외 LOGIN_CONNECTION_ERROR(HttpStatus.BAD_REQUEST, "로그인 요청 오류"), diff --git a/src/main/java/igoMoney/BE/controller/FCMMessageController.java b/src/main/java/igoMoney/BE/controller/FCMMessageController.java new file mode 100644 index 0000000..5205e45 --- /dev/null +++ b/src/main/java/igoMoney/BE/controller/FCMMessageController.java @@ -0,0 +1,40 @@ +package igoMoney.BE.controller; + +import com.google.firebase.messaging.Message; +import com.google.firebase.messaging.Notification; +import igoMoney.BE.service.FCMTokenService; +import igoMoney.BE.service.UserService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@Slf4j +@RequestMapping("fcm") +public class FCMMessageController { + + private final FCMTokenService fcmTokenService; + private final UserService userService; + + @PostMapping("") + public ResponseEntity sendPushAlarm(@RequestParam(value="fcmToken") String fcmToken, + @RequestParam(value="title") String title, + @RequestParam(value="content") String content){ + + Message message = Message.builder() + .setNotification(Notification.builder() + .setTitle(title) + .setBody(content) + .build()) + .setToken(fcmToken) + .build(); + fcmTokenService.sendMessage(message); + return new ResponseEntity(HttpStatus.OK); + } +} diff --git a/src/main/java/igoMoney/BE/service/FCMTokenService.java b/src/main/java/igoMoney/BE/service/FCMTokenService.java index fe083f4..fe59f56 100644 --- a/src/main/java/igoMoney/BE/service/FCMTokenService.java +++ b/src/main/java/igoMoney/BE/service/FCMTokenService.java @@ -2,26 +2,35 @@ import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.Message; -import igoMoney.BE.domain.Notification; +import com.google.firebase.messaging.Notification; +import igoMoney.BE.common.exception.CustomException; +import igoMoney.BE.common.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Repository; +import java.util.concurrent.ExecutionException; + @Repository @RequiredArgsConstructor public class FCMTokenService { private final StringRedisTemplate tokenRedisTemplate; - public void sendNotification(Notification notification) { - Long userId = notification.getUser().getId(); - if(!hasKey(userId)){ return;} + public void sendNotification(String title, String content, Long userId) { + if(!hasKey(userId)){ + throw new CustomException(ErrorCode.FCM_TOKEN_INVALID); + } String token = getToken(userId); + Notification fcmNotification = Notification.builder() + .setTitle(title) + .setBody(content) + .build(); + Message message = Message.builder() - .putData("title", notification.getTitle()) - .putData("content", notification.getMessage()) - .putData("userId", String.valueOf(userId)) .setToken(token) + .setNotification(fcmNotification) + .putData("userId", String.valueOf(userId)) .build(); sendMessage(message); } @@ -43,7 +52,12 @@ public boolean hasKey(Long userId){ return tokenRedisTemplate.hasKey(String.valueOf(userId)); } - private void sendMessage(Message message) { - FirebaseMessaging.getInstance().sendAsync(message); + public void sendMessage(Message message) { + try { + FirebaseMessaging.getInstance().sendAsync(message).get(); + } catch (ExecutionException | InterruptedException e) { + e.printStackTrace(); + throw new CustomException(ErrorCode.FCM_MESSAGE_FAILED); + } } } diff --git a/src/main/java/igoMoney/BE/service/NotificationService.java b/src/main/java/igoMoney/BE/service/NotificationService.java index 1b55e94..1f4b235 100644 --- a/src/main/java/igoMoney/BE/service/NotificationService.java +++ b/src/main/java/igoMoney/BE/service/NotificationService.java @@ -18,7 +18,7 @@ public class NotificationService { public void makeNotification(Notification notification){ notificationRepository.save(notification); - fcmTokenService.sendNotification(notification); + fcmTokenService.sendNotification(notification.getTitle(), notification.getMessage() , notification.getUser().getId()); } }