From c361f62a0e911ed9fb174f06441c7fb9b6c7c058 Mon Sep 17 00:00:00 2001 From: yurim0628 Date: Mon, 10 Jun 2024 21:48:47 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20#149=20type=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sabujak/fcm/constants/FCMConstants.java | 1 + .../fcm/service/FCMNotificationEventListener.java | 14 +++++++------- .../fcm/service/FCMNotificationService.java | 12 ++++++++---- .../notification/dto/NotificationResponse.java | 3 +++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/example/sabujak/fcm/constants/FCMConstants.java b/src/main/java/com/example/sabujak/fcm/constants/FCMConstants.java index 960fd08..98d553c 100644 --- a/src/main/java/com/example/sabujak/fcm/constants/FCMConstants.java +++ b/src/main/java/com/example/sabujak/fcm/constants/FCMConstants.java @@ -8,6 +8,7 @@ public class FCMConstants { // Target URL public static final String TARGET_ID_KEY = "targetId"; + public static final String TARGET_TYPE_KEY = "targetType"; // Notification Title public static final String COMMENT_TITLE = "내 글에 댓글"; diff --git a/src/main/java/com/example/sabujak/fcm/service/FCMNotificationEventListener.java b/src/main/java/com/example/sabujak/fcm/service/FCMNotificationEventListener.java index 58df8e3..8d78b33 100644 --- a/src/main/java/com/example/sabujak/fcm/service/FCMNotificationEventListener.java +++ b/src/main/java/com/example/sabujak/fcm/service/FCMNotificationEventListener.java @@ -43,7 +43,7 @@ public void saveAndSendFCMNotificationForComment(SaveCommentEvent event) { "Writer Email: [{}], Notification Content: [{}], Target URL: [{}]", email, content, targetId); saveNotification(event.commenterImage(), COMMENT_TITLE, content, targetId, COMMUNITY, event.writer()); - sendFCMNotification(email, createFCMMessage(email, COMMENT_TITLE, content, targetId)); + sendFCMNotification(email, createFCMMessage(email, COMMENT_TITLE, content, targetId, COMMUNITY)); } @TransactionalEventListener(phase = AFTER_COMMIT) @@ -65,7 +65,7 @@ public void saveAndSendFCMNotificationForMeetingRoomInvitation(ReserveMeetingRoo log.info("Notification Target Participant Email: [{}]", email); saveNotification(CHECK_IMAGE, MEETING_ROOM_INVITATION_TITLE, content, targetId, RESERVATION, participant); - sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_INVITATION_TITLE, content, targetId)); + sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_INVITATION_TITLE, content, targetId, RESERVATION)); } } @@ -88,7 +88,7 @@ public void saveAndSendFCMNotificationForRechargingRoomCancellation(ReserveMeeti log.info("Notification Content: [{}], Notification Target Canceler Email: [{}]", content, email); saveNotification(CHECK_IMAGE, RECHARGING_ROOM_CANCELLATION_TITLE, content, targetId, RESERVATION, canceler); - sendFCMNotificationAsync(email, createFCMMessage(email, RECHARGING_ROOM_CANCELLATION_TITLE, content, targetId)); + sendFCMNotificationAsync(email, createFCMMessage(email, RECHARGING_ROOM_CANCELLATION_TITLE, content, targetId, RESERVATION)); } } @@ -104,7 +104,7 @@ public void saveAndSendFCMNotificationForMeetingRoomEntry(FindMeetingRoomEntryNo log.info("Notification Target Member Email: [{}]", email); saveNotification(WARING_IMAGE, MEETING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION, member); - sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_RESERVATION_TITLE, content, targetId)); + sendFCMNotificationAsync(email, createFCMMessage(email, MEETING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION)); } } @@ -117,7 +117,7 @@ public void saveAndSendFCMNotificationForRechargingRoomEntry(FindRechargingRoomE "Member Email: [{}], Notification Content: [{}], Target ID: [{}]", email, content, targetId); saveNotification(WARING_IMAGE, RECHARGING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION, event.member()); - sendFCMNotification(email, createFCMMessage(email, RECHARGING_ROOM_RESERVATION_TITLE, content, targetId)); + sendFCMNotification(email, createFCMMessage(email, RECHARGING_ROOM_RESERVATION_TITLE, content, targetId, RESERVATION)); } private void saveNotification(String image, String title, String content, Long targetId, NotificationType type, Member member) { @@ -125,8 +125,8 @@ private void saveNotification(String image, String title, String content, Long t notificationService.saveNotification(image, title, content, targetId, type, member); } - private Message createFCMMessage(String email, String title, String content, Long targetId) { - return fcmNotificationService.createFCMMessage(email, title, content, targetId); + private Message createFCMMessage(String email, String title, String content, Long targetId, NotificationType targetType) { + return fcmNotificationService.createFCMMessage(email, title, content, targetId, targetType); } private void sendFCMNotification(String email, Message message) { diff --git a/src/main/java/com/example/sabujak/fcm/service/FCMNotificationService.java b/src/main/java/com/example/sabujak/fcm/service/FCMNotificationService.java index 6ab01f8..6441c48 100644 --- a/src/main/java/com/example/sabujak/fcm/service/FCMNotificationService.java +++ b/src/main/java/com/example/sabujak/fcm/service/FCMNotificationService.java @@ -1,6 +1,7 @@ package com.example.sabujak.fcm.service; import com.example.sabujak.fcm.exception.FCMException; +import com.example.sabujak.notification.entity.NotificationType; import com.google.api.core.ApiFuture; import com.google.firebase.messaging.*; import lombok.RequiredArgsConstructor; @@ -30,11 +31,11 @@ public class FCMNotificationService { private final Executor callBackTaskExecutor; private final ObjectProvider provider; - public Message createFCMMessage(String email, String title, String content, Long targetId) { + public Message createFCMMessage(String email, String title, String content, Long targetId, NotificationType targetType) { return Message.builder() .setToken(fcmTokenService.getFCMToken(email)) .setNotification(createNotification(title, content)) - .putAllData(createData(targetId)) + .putAllData(createData(targetId, targetType)) .build(); } @@ -46,8 +47,11 @@ private Notification createNotification(String title, String body) { .build(); } - private Map createData(Long targetId) { - return Map.of(TARGET_ID_KEY, targetId.toString()); + private Map createData(Long targetId, NotificationType targetType) { + return Map.of( + TARGET_ID_KEY, targetId.toString(), + TARGET_TYPE_KEY, targetType.toString() + ); } public void sendFCMNotificationAsync(String email, Message message) { diff --git a/src/main/java/com/example/sabujak/notification/dto/NotificationResponse.java b/src/main/java/com/example/sabujak/notification/dto/NotificationResponse.java index 74e5949..d2c0ca9 100644 --- a/src/main/java/com/example/sabujak/notification/dto/NotificationResponse.java +++ b/src/main/java/com/example/sabujak/notification/dto/NotificationResponse.java @@ -1,6 +1,7 @@ package com.example.sabujak.notification.dto; import com.example.sabujak.notification.entity.Notification; +import com.example.sabujak.notification.entity.NotificationType; import java.time.LocalDateTime; @@ -10,6 +11,7 @@ public record NotificationResponse( String title, String content, Long targetId, + NotificationType targetType, LocalDateTime date ) { @@ -20,6 +22,7 @@ public static NotificationResponse of(Notification notification) { notification.getTitle(), notification.getContent(), notification.getTargetId(), + notification.getType(), notification.getCreatedDate() ); }