From 4bc01fd0350874e8e141cd8fec1618cd13387246 Mon Sep 17 00:00:00 2001 From: kanguk Date: Wed, 13 Nov 2024 10:40:07 +0900 Subject: [PATCH] =?UTF-8?q?bug:=20=EB=A1=9C=EA=B9=85=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20=EB=B0=8F=20=EC=95=8C=EB=A6=BC=20=ED=98=95=EC=8B=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 테스트 알림도 삭제 --- .../controller/NotificationController.java | 19 ------- .../scheduler/NotificationScheduler.java | 19 +------ .../service/NotificationService.java | 50 +++++-------------- 3 files changed, 14 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/com/splanet/splanet/notification/controller/NotificationController.java diff --git a/src/main/java/com/splanet/splanet/notification/controller/NotificationController.java b/src/main/java/com/splanet/splanet/notification/controller/NotificationController.java deleted file mode 100644 index 72624a5c..00000000 --- a/src/main/java/com/splanet/splanet/notification/controller/NotificationController.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.splanet.splanet.notification.controller; - -import com.splanet.splanet.notification.service.NotificationService; -import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -@RestController -@RequiredArgsConstructor -public class NotificationController implements NotificationApi{ - - private final NotificationService notificationService; - - @Override - public ResponseEntity sendTestNotification(@PathVariable Long userId) { - notificationService.sendTestNotification(userId); - return ResponseEntity.ok("테스트 알림 전송 완료: " + userId); - } -} \ No newline at end of file diff --git a/src/main/java/com/splanet/splanet/notification/scheduler/NotificationScheduler.java b/src/main/java/com/splanet/splanet/notification/scheduler/NotificationScheduler.java index 8ffa0e9e..17fe6dd8 100644 --- a/src/main/java/com/splanet/splanet/notification/scheduler/NotificationScheduler.java +++ b/src/main/java/com/splanet/splanet/notification/scheduler/NotificationScheduler.java @@ -26,29 +26,24 @@ @RequiredArgsConstructor public class NotificationScheduler { + private static final ZoneId ZONE_ID_SEOUL = ZoneId.of("Asia/Seoul"); private final PlanRepository planRepository; private final FcmTokenRepository fcmTokenRepository; private final NotificationLogRepository notificationLogRepository; private final NotificationService notificationService; private final QueryPerformanceService queryPerformanceService; - private static final ZoneId ZONE_ID_SEOUL = ZoneId.of("Asia/Seoul"); - @Scheduled(fixedRate = 60000) public void sendScheduledNotifications() { LocalDateTime now = LocalDateTime.now(ZONE_ID_SEOUL); - log.info("Scheduler started at {}", now); List upcomingPlans = planRepository.findUpcomingPlans(now); - log.info("Found {} upcoming plans", upcomingPlans.size()); Set userIds = upcomingPlans.stream() .map(plan -> plan.getUser().getId()) .collect(Collectors.toSet()); - log.info("Collected user IDs: {}", userIds); List allFcmTokens = fcmTokenRepository.findByUserIdIn(userIds); - log.info("Fetched {} FCM tokens", allFcmTokens.size()); Map> userFcmTokenMap = allFcmTokens.stream() .collect(Collectors.groupingBy(fcmToken -> fcmToken.getUser().getId())); @@ -56,15 +51,12 @@ public void sendScheduledNotifications() { Set planIds = upcomingPlans.stream() .map(Plan::getId) .collect(Collectors.toSet()); - log.info("Collected plan IDs: {}", planIds); Set fcmTokenIds = allFcmTokens.stream() .map(FcmToken::getId) .collect(Collectors.toSet()); - log.info("Collected FCM token IDs: {}", fcmTokenIds); List notificationLogs = notificationLogRepository.findByFcmTokenIdInAndPlanIdIn(fcmTokenIds, planIds); - log.info("Found {} notification logs for existing notifications", notificationLogs.size()); Set sentNotificationKeys = notificationLogs.stream() .map(log -> log.getFcmToken().getId() + ":" + log.getPlan().getId()) @@ -73,27 +65,18 @@ public void sendScheduledNotifications() { for (Plan plan : upcomingPlans) { Long userId = plan.getUser().getId(); List fcmTokens = userFcmTokenMap.getOrDefault(userId, Collections.emptyList()); - log.info("Processing plan ID {} for user ID {}, FCM tokens: {}", plan.getId(), userId, fcmTokens.size()); for (FcmToken fcmToken : fcmTokens) { if (Boolean.TRUE.equals(fcmToken.getIsNotificationEnabled())) { LocalDateTime notificationTime = plan.getStartDate().atZone(ZONE_ID_SEOUL).toLocalDateTime().minusMinutes(fcmToken.getNotificationOffset()); - log.info("Evaluating notification for FCM token ID {} at notificationTime: {}", fcmToken.getId(), notificationTime); if (notificationTime.isAfter(now.minusMinutes(5)) && notificationTime.isBefore(now.plusMinutes(1))) { String notificationKey = fcmToken.getId() + ":" + plan.getId(); if (!sentNotificationKeys.contains(notificationKey)) { - log.info("Sending notification for FCM token ID {} and plan ID {}", fcmToken.getId(), plan.getId()); notificationService.sendNotification(fcmToken, plan); - } else { - log.info("Notification already sent for FCM token ID {} and plan ID {}", fcmToken.getId(), plan.getId()); } - } else { - log.info("Notification time {} does not match the required range for now: {}", notificationTime, now); } - } else { - log.info("Notification disabled for FCM token ID {}", fcmToken.getId()); } } } diff --git a/src/main/java/com/splanet/splanet/notification/service/NotificationService.java b/src/main/java/com/splanet/splanet/notification/service/NotificationService.java index 42167a37..500289ac 100644 --- a/src/main/java/com/splanet/splanet/notification/service/NotificationService.java +++ b/src/main/java/com/splanet/splanet/notification/service/NotificationService.java @@ -12,7 +12,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; -import java.util.List; +import java.time.format.DateTimeFormatter; @Slf4j @Service @@ -29,53 +29,29 @@ public NotificationService(FcmTokenRepository fcmTokenRepository, FirebaseMessag } public void sendNotification(FcmToken fcmToken, Plan plan) { - String title = "곧 시작하는 플랜: " + plan.getTitle(); - String body = "곧 시작하는 플랜이 있어요! " + plan.getDescription(); + DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH시 mm분"); + + String nickname = plan.getUser().getNickname(); + + String startTime = plan.getStartDate().toLocalTime().format(timeFormatter); + String endTime = plan.getEndDate().toLocalTime().format(timeFormatter); + + String title = "🗓️ " + nickname + "님! " + plan.getTitle() + " 시간이에요! "; + String body = startTime + " - " + endTime + " \n" + + (plan.getDescription() != null ? plan.getDescription() : " "); Notification notification = new Notification(title, body); - Message message = Message.builder() - .setToken(fcmToken.getToken()) - .setNotification(notification) - .putData("title", plan.getTitle()) - .putData("title", plan.getDescription()) - .putData("startDate", plan.getStartDate().toString()) - .build(); + Message message = Message.builder().setToken(fcmToken.getToken()).setNotification(notification).putData("title", plan.getTitle()).putData("title", plan.getDescription()).putData("startDate", plan.getStartDate().toString()).build(); try { String response = firebaseMessaging.send(message); log.info("알림을 정상적으로 전송하였습니다. : {}", response); - // 알림 전송 기록 저장 - NotificationLog logEntry = NotificationLog.builder() - .fcmToken(fcmToken) - .plan(plan) - .sentAt(LocalDateTime.now()) - .build(); + NotificationLog logEntry = NotificationLog.builder().fcmToken(fcmToken).plan(plan).sentAt(LocalDateTime.now()).build(); notificationLogRepository.save(logEntry); } catch (Exception e) { log.error("FCM 알림 전송 실패 ", e); } } - - // 알림 테스트를 위한 메소드 (추후 삭제) - public void sendTestNotification(Long userId) { - List fcmTokens = fcmTokenRepository.findByUserId(userId); - - for (FcmToken fcmToken : fcmTokens) { - Notification notification = new Notification("테스트 알림", "이것은 테스트 알림입니다."); - - Message message = Message.builder() - .setToken(fcmToken.getToken()) - .setNotification(notification) - .build(); - - try { - String response = firebaseMessaging.send(message); - log.info("Successfully sent message: {}", response); - } catch (Exception e) { - log.error("Failed to send FCM notification", e); - } - } - } }