From 4bd25f9b73c25e77e428cce414c738bbde4e15d3 Mon Sep 17 00:00:00 2001 From: Sin Ye Rin <91180366+nyeroni@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:34:55 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=95=8C=EB=A6=BC=20=EC=8B=9C?= =?UTF-8?q?=EA=B0=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yerong/wedle/common/config/FMCConfig.java | 23 ++++--- .../yerong/wedle/common/utils/FcmUtils.java | 60 +++++++++++++------ .../service/NotificationService.java | 2 +- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main/java/yerong/wedle/common/config/FMCConfig.java b/src/main/java/yerong/wedle/common/config/FMCConfig.java index ae48faa..98a9743 100644 --- a/src/main/java/yerong/wedle/common/config/FMCConfig.java +++ b/src/main/java/yerong/wedle/common/config/FMCConfig.java @@ -6,25 +6,32 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import javax.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; @Configuration +@Slf4j public class FMCConfig { @Value("${firebase.config}") private String firebaseConfig; @PostConstruct - private void init() throws IOException { - ByteArrayInputStream serviceAccountStream = new ByteArrayInputStream(firebaseConfig.getBytes()); - - FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(GoogleCredentials.fromStream(serviceAccountStream)) - .build(); - - FirebaseApp.initializeApp(options); + private void init() { + try (ByteArrayInputStream serviceAccount = new ByteArrayInputStream(firebaseConfig.getBytes())) { + FirebaseOptions options = FirebaseOptions.builder() + .setCredentials(GoogleCredentials.fromStream(serviceAccount)) + .build(); + + FirebaseApp.initializeApp(options); + log.info("파이어베이스 서버와의 연결에 성공했습니다."); + } catch (IOException e) { + log.error("파이어베이스 서버와의 연결에 실패했습니다.", e); + } } } diff --git a/src/main/java/yerong/wedle/common/utils/FcmUtils.java b/src/main/java/yerong/wedle/common/utils/FcmUtils.java index 7b2f710..58b33ff 100644 --- a/src/main/java/yerong/wedle/common/utils/FcmUtils.java +++ b/src/main/java/yerong/wedle/common/utils/FcmUtils.java @@ -10,6 +10,7 @@ import java.util.Date; import java.util.List; + import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -25,33 +26,53 @@ public class FcmUtils { public static void broadCast(final List registrationTokens, String title, String body) { limitSizeValidate(registrationTokens); - MulticastMessage message = MulticastMessage.builder() + log.info("registrationTokens = {}", registrationTokens.get(0)); + log.info("title = {}", title); + log.info("body = {}", body); + + if (title == null || title.isEmpty()) { + log.error("제목이 비어 있습니다."); + return; + } + if (body == null || body.isEmpty()) { + log.error("본문이 비어 있습니다."); + return; + } + + long currentTime = new Date().getTime() / 1000; + if (EXPIRED_TIME_FOR_UNIX <= currentTime) { + log.error("APNs 만료 시간 설정이 잘못되었습니다."); + } + + MulticastMessage message = buildMessage(registrationTokens, title, body); + + try { + BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message); + pushSuccessValidate(registrationTokens, response); + } catch (FirebaseMessagingException e) { + log.error("FCM 메시지 전송 중 예외 발생: {}", e.getMessage(), e); + if (e.getErrorCode() != null) { + log.error("Error Code: {}", e.getErrorCode()); + } + } + } + + private static MulticastMessage buildMessage(List registrationTokens, String title, String body) { + return MulticastMessage.builder() .setNotification(Notification.builder() .setTitle(title) .setBody(body) .build()) .setApnsConfig(ApnsConfig.builder() - .setAps(Aps - .builder() + .setAps(Aps.builder() .setAlert(body) .build()) .putHeader("apns-expiration", Long.toString(EXPIRED_TIME_FOR_UNIX)) .build()) .addAllTokens(registrationTokens) .build(); - - try { - BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message); - pushSuccessValidate(registrationTokens, response); - } catch (FirebaseMessagingException e) { - log.error("FCM 메시지 전송 중 예외 발생: {}", e.getMessage(), e); - for (String token : registrationTokens) { - log.error("유효하지 않은 토큰: {}", token); - } - } } - private static void limitSizeValidate(final List registrationTokens) { if (registrationTokens.size() > FCM_PUSH_LIMIT_SIZE) { throw new IllegalArgumentException("FCM push 알림 수신자는 최대 500명입니다."); @@ -59,13 +80,16 @@ private static void limitSizeValidate(final List registrationTokens) { } private static void pushSuccessValidate(final List registrationTokens, final BatchResponse response) { - System.out.println(response.getSuccessCount() + " messages were sent successfully."); + log.info("{} messages were sent successfully.", response.getSuccessCount()); if (response.getFailureCount() > 0) { - System.out.println(response.getFailureCount() + " messages failed to send."); + log.error("{} messages failed to send.", response.getFailureCount()); response.getResponses().forEach(sendResponse -> { if (!sendResponse.isSuccessful()) { - System.out.println("Failed to send message to token: " + sendResponse.getMessageId()); - System.out.println("Error: " + sendResponse.getException().getMessage()); + String messageId = sendResponse.getMessageId(); + String errorMessage = sendResponse.getException() != null ? sendResponse.getException().getMessage() : "Unknown error"; + + log.error("Failed to send message to token: {}. Error: {}", + messageId != null ? messageId : "null", errorMessage); } }); } diff --git a/src/main/java/yerong/wedle/notification/service/NotificationService.java b/src/main/java/yerong/wedle/notification/service/NotificationService.java index f6bcd9d..4127475 100644 --- a/src/main/java/yerong/wedle/notification/service/NotificationService.java +++ b/src/main/java/yerong/wedle/notification/service/NotificationService.java @@ -71,7 +71,7 @@ private CalendarEvent getCalendarEventById(Long calendarId) { } @Transactional - @Scheduled(cron = "0 18 19 * * ?", zone = "Asia/Seoul") + @Scheduled(cron = "0 42 22 * * ?", zone = "Asia/Seoul") public void sendNotifications() { LocalDate today = LocalDate.now(); List dueNotifications = notificationRepository.findByNotificationDate(today);