From a4250ea267c58d64a84eb2364588855cf8cd79ab Mon Sep 17 00:00:00 2001 From: hye-on Date: Thu, 18 Apr 2024 21:22:24 +0900 Subject: [PATCH 1/7] =?UTF-8?q?#106=20[RESTRUCTURING]=20:=20event=EB=A5=BC?= =?UTF-8?q?=20=EC=9D=B4=EC=9A=A9=ED=95=B4=EC=84=9C=20fcm=20push=EB=B3=B4?= =?UTF-8?q?=EB=82=B4=EA=B2=8C=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=EB=9F=AD?= =?UTF-8?q?=EC=B3=90=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NotificationEventListener.java | 21 +++++++++++ .../NotificationEventPublisher.java | 17 +++++++++ ...rvice.java => NotificationLogService.java} | 2 +- .../api/notification/NotificationService.java | 21 ++++++----- .../togetUp/api/users/UserEventHandler.java | 26 +++++++++++++ .../togetUp/api/users/UserService.java | 7 ++++ .../users/fcmToken/FcmTokenDeleteEvent.java | 12 ++++++ .../users/fcmToken/FcmTokenRepository.java | 3 +- .../infra/{google => }/fcm/FcmService.java | 37 ++++++++++++++----- 9 files changed, 125 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventListener.java create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java rename src/main/java/com/wakeUpTogetUp/togetUp/api/notification/{PushLogService.java => NotificationLogService.java} (82%) create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserEventHandler.java create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenDeleteEvent.java rename src/main/java/com/wakeUpTogetUp/togetUp/infra/{google => }/fcm/FcmService.java (71%) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventListener.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventListener.java new file mode 100644 index 00000000..03ac5f5c --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventListener.java @@ -0,0 +1,21 @@ +package com.wakeUpTogetUp.togetUp.api.notification; + +import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo; +import com.wakeUpTogetUp.togetUp.infra.fcm.FcmService; +import lombok.RequiredArgsConstructor; +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + + +@Component +@RequiredArgsConstructor +public class NotificationEventListener { + private final FcmService fcmService; + + @Async() + @EventListener(NotificationSendVo.class) + public void handleSendPushEvent(NotificationSendVo event) { + fcmService.sendMessageToTokens(event); + } +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java new file mode 100644 index 00000000..983b2f80 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java @@ -0,0 +1,17 @@ +package com.wakeUpTogetUp.togetUp.api.notification; + +import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo; +import lombok.RequiredArgsConstructor; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class NotificationEventPublisher { + private final ApplicationEventPublisher eventPublisher; + + public void publishNotificationSendEvent(NotificationSendVo notificationSendVo) { + eventPublisher.publishEvent(notificationSendVo); + } +} + diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/PushLogService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationLogService.java similarity index 82% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/notification/PushLogService.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationLogService.java index 843e1cb6..c6f02f6c 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/PushLogService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationLogService.java @@ -7,6 +7,6 @@ @Service @RequiredArgsConstructor -public class PushLogService { +public class NotificationLogService { } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java index 4a2bf4c7..f69b0d9f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java @@ -11,10 +11,12 @@ import com.wakeUpTogetUp.togetUp.api.users.model.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; -import com.wakeUpTogetUp.togetUp.infra.google.fcm.FcmService; +import com.wakeUpTogetUp.togetUp.infra.fcm.FcmService; + import java.util.List; import java.util.Map; import java.util.stream.Collectors; + import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -26,16 +28,16 @@ public class NotificationService { private final RoomRepository roomRepository; private final UserRepository userRepository; private final FcmTokenRepository fcmTokenRepository; + private final NotificationEventPublisher notificationEventPublisher; public void sendNotificationToAllUsers(String title, String body) { - fcmService.sendMessageToTokens( - new NotificationSendVo( - fcmTokenRepository.findAllByUser_IdIn(userService.getAgreedNotiUsersIds()), - title, - body, - Map.of(DataKeyType.LINK.getKey(), DataValueType.HOME.toString()) - ) - ); + notificationEventPublisher.publishNotificationSendEvent(new NotificationSendVo( + fcmTokenRepository.findAllByUser_IdIn(userService.getAgreedNotiUsersIds()), + title, + body, + Map.of(DataKeyType.LINK.getKey(), DataValueType.HOME.toString()) + )); + } public void sendNotificationToUsersInRoom(Integer alarmId, Integer missionCompleteUserId) { @@ -51,6 +53,7 @@ public void sendNotificationToUsersInRoom(Integer alarmId, Integer missionComple .collect(Collectors.toList()); if (userIdsInRoom.size() > 0) { + fcmService.sendMessageToTokens( new RoomMissionLogNotificationVo( user, diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserEventHandler.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserEventHandler.java new file mode 100644 index 00000000..1270bef1 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserEventHandler.java @@ -0,0 +1,26 @@ +package com.wakeUpTogetUp.togetUp.api.users; + + +import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenDeleteEvent; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + + +@Slf4j +@Component +@Transactional +@RequiredArgsConstructor +public class UserEventHandler { + + private final UserService userService; + + @EventListener(FcmTokenDeleteEvent.class) + public void handlePushTokenDeleteEvent(FcmTokenDeleteEvent event) { + userService.deleteFcmTokens(event.getFcmTokens()); + } + +} + diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java index 936d4c42..95c44d19 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java @@ -12,9 +12,11 @@ import com.wakeUpTogetUp.togetUp.api.users.vo.UserProgressResult; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; + import java.util.List; import java.util.Objects; import java.util.stream.Collectors; + import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -68,6 +70,11 @@ private void saveFcmToken(User user, String fcmTokenValue) { fcmTokenRepository.save(fcmToken); } + @Transactional + public void deleteFcmTokens(List fcmTokenValues) { + fcmTokenRepository.deleteByValueIn(fcmTokenValues); + } + public void updateAgreePush(Integer userId, boolean agreePush) { User user = findExistingUser(userRepository, userId); user.changeAgreePush(agreePush); diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenDeleteEvent.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenDeleteEvent.java new file mode 100644 index 00000000..0a7e4116 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenDeleteEvent.java @@ -0,0 +1,12 @@ +package com.wakeUpTogetUp.togetUp.api.users.fcmToken; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +@Getter +@RequiredArgsConstructor +public class FcmTokenDeleteEvent { + private final List fcmTokens; +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java index a82cf9ff..b36fb531 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java @@ -10,7 +10,8 @@ public interface FcmTokenRepository extends JpaRepository { List findAllByUser_IdIn(List userIds); - void deleteAllByValueIn(List values); + + void deleteByValueIn(List fcmTokenValues); boolean existsByValue(String fcmTokenValue); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/infra/google/fcm/FcmService.java b/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java similarity index 71% rename from src/main/java/com/wakeUpTogetUp/togetUp/infra/google/fcm/FcmService.java rename to src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java index 6beaba2d..dce45c89 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/infra/google/fcm/FcmService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java @@ -1,47 +1,65 @@ -package com.wakeUpTogetUp.togetUp.infra.google.fcm; +package com.wakeUpTogetUp.togetUp.infra.fcm; import com.google.api.core.ApiFuture; import com.google.firebase.messaging.*; import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; -import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenRepository; +import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenDeleteEvent; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; + import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; import java.util.stream.IntStream; +@Slf4j @Service @RequiredArgsConstructor public class FcmService { private final FirebaseMessaging firebaseMessaging; - private final FcmTokenRepository fcmTokenRepository; + private final ApplicationEventPublisher eventPublisher; public void sendMessageToTokens(NotificationSendVo notificationSendVo) { - // message 만들기 + MulticastMessage message = getPreconfiguredMessageToTokens(notificationSendVo); ApiFuture response = sendAndGetResponse(message); - deleteInvalidToken(response, notificationSendVo.getFcmTokens().stream() .map(FcmToken::getValue) .collect(Collectors.toList())); } + public void deleteInvalidToken(ApiFuture response, List deviceTokens) { try { if (response.get().getFailureCount() > 0) { List failedDeviceTokens = new ArrayList<>(); List responses = response.get().getResponses(); - IntStream.range(0, responses.size()) .filter(idx -> !responses.get(idx).isSuccessful()) - .forEach(idx -> failedDeviceTokens.add(deviceTokens.get(idx))); + .forEach(idx -> { + + String token = deviceTokens.get(idx); + MessagingErrorCode errorCode = responses.get(idx).getException().getMessagingErrorCode(); + String errorMessage = responses.get(idx).getException().getMessage(); + + if (errorCode == MessagingErrorCode.UNREGISTERED) + failedDeviceTokens.add(deviceTokens.get(idx)); + + log.warn("[PushException][{}]{} {}", + token, + errorCode, + errorMessage); + + + }); - fcmTokenRepository.deleteAllByValueIn(failedDeviceTokens); + eventPublisher.publishEvent(new FcmTokenDeleteEvent(failedDeviceTokens)); } } catch (InterruptedException e) { throw new RuntimeException(e); @@ -51,12 +69,11 @@ public void deleteInvalidToken(ApiFuture response, List d } private ApiFuture sendAndGetResponse(MulticastMessage message) { - ApiFuture response = FirebaseMessaging.getInstance().sendMulticastAsync(message); + ApiFuture response = firebaseMessaging.sendMulticastAsync(message); return response; } - // token 여러 개 private MulticastMessage getPreconfiguredMessageToTokens(NotificationSendVo notificationSendVo) { return getPreconfiguredMulticastMessageBuilder(notificationSendVo).addAllTokens(notificationSendVo.getFcmTokens().stream() .map(FcmToken::getValue) From 80e1076a8f180c2ad245c495332f548a9f319817 Mon Sep 17 00:00:00 2001 From: hye-on Date: Thu, 18 Apr 2024 21:22:52 +0900 Subject: [PATCH 2/7] =?UTF-8?q?[CHORE]=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=BD=94=EB=93=9C,=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notification/NotificationProvider.java | 11 -------- .../dto/response/PushNotificationRes.java | 26 ------------------- .../api/notification/entity/Notification.java | 14 +++------- 3 files changed, 3 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationProvider.java delete mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/notification/dto/response/PushNotificationRes.java diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationProvider.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationProvider.java deleted file mode 100644 index c66f439b..00000000 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationProvider.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.wakeUpTogetUp.togetUp.api.notification; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -@Service -@RequiredArgsConstructor -public class NotificationProvider { - private final NotificationRepository notificationRepository; - -} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/dto/response/PushNotificationRes.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/dto/response/PushNotificationRes.java deleted file mode 100644 index 7707fe14..00000000 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/dto/response/PushNotificationRes.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.wakeUpTogetUp.togetUp.api.notification.dto.response; - -import lombok.*; - -@Getter -@Setter -@NoArgsConstructor -@Builder -public class PushNotificationRes { - private String message; - private String category; - private String response; - private String topic; - - public PushNotificationRes(String message, String category, String response) { - this.message = message; - this.category = category; - this.response = response; - } - public PushNotificationRes(String message, String category, String response, String topic) { - this.message = message; - this.category = category; - this.response = response; - this.topic = topic; - } -} \ No newline at end of file diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/entity/Notification.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/entity/Notification.java index 0ab50e64..ee681358 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/entity/Notification.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/entity/Notification.java @@ -19,17 +19,9 @@ public class Notification { - @Builder - public Notification(String title, String content, Room room, FcmToken fcmToken) { - this.title = title; - this.content = content; - this.room = room; - this.fcmToken = fcmToken; - } - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column( columnDefinition = "INT UNSIGNED") + @Column(columnDefinition = "INT UNSIGNED") private Integer id; private String title; @@ -40,10 +32,10 @@ public Notification(String title, String content, Room room, FcmToken fcmToken) private String createdAt = TimeFormatter.timestampFormat(new Timestamp(System.currentTimeMillis())); @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "roomId",columnDefinition = "INT UNSIGNED") + @JoinColumn(name = "roomId", columnDefinition = "INT UNSIGNED") private Room room; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "fcmTokenId",columnDefinition = "INT UNSIGNED") + @JoinColumn(name = "fcmTokenId", columnDefinition = "INT UNSIGNED") private FcmToken fcmToken; } From 03d511d66216a6bf7203b909d968cc3b415a0c2c Mon Sep 17 00:00:00 2001 From: hye-on Date: Thu, 18 Apr 2024 21:24:04 +0900 Subject: [PATCH 3/7] =?UTF-8?q?[FIX]=20:=20=EC=88=9C=ED=99=98=EC=A2=85?= =?UTF-8?q?=EC=86=8D=EC=84=B1=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/wakeUpTogetUp/togetUp/api/users/model/User.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java index 257a5cb1..34a26f0f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java @@ -80,6 +80,7 @@ public class User { @Column(name = "is_deleted") private boolean isDeleted; + @JsonIgnore @OneToMany(mappedBy = "user") private List fcmToken = new ArrayList<>(); From f4f9cf34f5077e297003112f86cd287b69350fe4 Mon Sep 17 00:00:00 2001 From: hye-on Date: Thu, 18 Apr 2024 21:35:14 +0900 Subject: [PATCH 4/7] =?UTF-8?q?#106=20[RESTRUCTURING]=20:=20=EA=B7=B8?= =?UTF-8?q?=EB=A3=B9=EC=95=8C=EB=9E=8C=EC=9D=84=20=EC=9D=B4=EB=B2=A4?= =?UTF-8?q?=ED=8A=B8=EB=A1=9C=20fcm=20=EC=95=8C=EB=A6=BC=20=EB=B3=B4?= =?UTF-8?q?=EB=82=B4=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/notification/NotificationService.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java index f69b0d9f..42ebb1e3 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java @@ -53,14 +53,11 @@ public void sendNotificationToUsersInRoom(Integer alarmId, Integer missionComple .collect(Collectors.toList()); if (userIdsInRoom.size() > 0) { - - fcmService.sendMessageToTokens( - new RoomMissionLogNotificationVo( - user, - room, - fcmTokenRepository.findAllByUser_IdIn(userIdsInRoom) - ) - ); + notificationEventPublisher.publishNotificationSendEvent(new RoomMissionLogNotificationVo( + user, + room, + fcmTokenRepository.findAllByUser_IdIn(userIdsInRoom) + )); } } From 04b2077292ec8a482d94e92a5177783449c5c918 Mon Sep 17 00:00:00 2001 From: hye-on Date: Thu, 18 Apr 2024 21:40:41 +0900 Subject: [PATCH 5/7] =?UTF-8?q?#106=20[FIX]=20:=20=EC=9D=B4=EB=B2=A4?= =?UTF-8?q?=ED=8A=B8=EB=A6=AC=EC=8A=A4=EB=84=88=EB=A5=BC=20=EC=9D=B4?= =?UTF-8?q?=EB=B2=A4=ED=8A=B8=ED=95=B8=EB=93=A4=EB=9F=AC=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ficationEventListener.java => NotificationEventHandler.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/com/wakeUpTogetUp/togetUp/api/notification/{NotificationEventListener.java => NotificationEventHandler.java} (93%) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventListener.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventHandler.java similarity index 93% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventListener.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventHandler.java index 03ac5f5c..59b91d22 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventListener.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventHandler.java @@ -10,7 +10,7 @@ @Component @RequiredArgsConstructor -public class NotificationEventListener { +public class NotificationEventHandler { private final FcmService fcmService; @Async() From 05364bfd2a0ff2d62b25d40e04d98f9bacff9036 Mon Sep 17 00:00:00 2001 From: hye-on Date: Mon, 22 Apr 2024 13:13:41 +0900 Subject: [PATCH 6/7] =?UTF-8?q?#123=20[FIX]=20:=EB=A6=AC=EB=B7=B0=EB=B0=98?= =?UTF-8?q?=EC=98=81.=20vo=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NotificationEventHandler.java | 6 ++--- .../NotificationEventPublisher.java | 6 ++--- .../api/notification/NotificationService.java | 8 +++--- ...SendVo.java => NotificationSendEvent.java} | 4 +-- ...a => RoomMissionLogNotificationEvent.java} | 5 ++-- .../togetUp/infra/fcm/FcmService.java | 26 +++++++++---------- 6 files changed, 27 insertions(+), 28 deletions(-) rename src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/{NotificationSendVo.java => NotificationSendEvent.java} (76%) rename src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/{RoomMissionLogNotificationVo.java => RoomMissionLogNotificationEvent.java} (83%) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventHandler.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventHandler.java index 59b91d22..88b327c2 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventHandler.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventHandler.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.notification; -import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo; +import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent; import com.wakeUpTogetUp.togetUp.infra.fcm.FcmService; import lombok.RequiredArgsConstructor; import org.springframework.context.event.EventListener; @@ -14,8 +14,8 @@ public class NotificationEventHandler { private final FcmService fcmService; @Async() - @EventListener(NotificationSendVo.class) - public void handleSendPushEvent(NotificationSendVo event) { + @EventListener(NotificationSendEvent.class) + public void handleSendPushEvent(NotificationSendEvent event) { fcmService.sendMessageToTokens(event); } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java index 983b2f80..fd9ae17f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.notification; -import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo; +import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @@ -10,8 +10,8 @@ public class NotificationEventPublisher { private final ApplicationEventPublisher eventPublisher; - public void publishNotificationSendEvent(NotificationSendVo notificationSendVo) { - eventPublisher.publishEvent(notificationSendVo); + public void publishNotificationSendEvent(NotificationSendEvent notificationSendEvent) { + eventPublisher.publishEvent(notificationSendEvent); } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java index 42ebb1e3..b099c08f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java @@ -1,7 +1,7 @@ package com.wakeUpTogetUp.togetUp.api.notification; -import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo; -import com.wakeUpTogetUp.togetUp.api.notification.vo.RoomMissionLogNotificationVo; +import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent; +import com.wakeUpTogetUp.togetUp.api.notification.vo.RoomMissionLogNotificationEvent; import com.wakeUpTogetUp.togetUp.api.room.RoomRepository; import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser; @@ -31,7 +31,7 @@ public class NotificationService { private final NotificationEventPublisher notificationEventPublisher; public void sendNotificationToAllUsers(String title, String body) { - notificationEventPublisher.publishNotificationSendEvent(new NotificationSendVo( + notificationEventPublisher.publishNotificationSendEvent(new NotificationSendEvent( fcmTokenRepository.findAllByUser_IdIn(userService.getAgreedNotiUsersIds()), title, body, @@ -53,7 +53,7 @@ public void sendNotificationToUsersInRoom(Integer alarmId, Integer missionComple .collect(Collectors.toList()); if (userIdsInRoom.size() > 0) { - notificationEventPublisher.publishNotificationSendEvent(new RoomMissionLogNotificationVo( + notificationEventPublisher.publishNotificationSendEvent(new RoomMissionLogNotificationEvent( user, room, fcmTokenRepository.findAllByUser_IdIn(userIdsInRoom) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/NotificationSendVo.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/NotificationSendEvent.java similarity index 76% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/NotificationSendVo.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/NotificationSendEvent.java index 38becf9c..29fda766 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/NotificationSendVo.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/NotificationSendEvent.java @@ -8,13 +8,13 @@ @ToString @Getter -public class NotificationSendVo { +public class NotificationSendEvent { private final List fcmTokens; private final String title; private final String body; private final Map dataMap; - public NotificationSendVo(List fcmTokens, String title, String body, Map dataMap) { + public NotificationSendEvent(List fcmTokens, String title, String body, Map dataMap) { this.fcmTokens = fcmTokens; this.title = title; this.body = body; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationVo.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationEvent.java similarity index 83% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationVo.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationEvent.java index 973fe7f8..d8e7711a 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationVo.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationEvent.java @@ -7,16 +7,15 @@ import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; import com.wakeUpTogetUp.togetUp.api.users.model.User; -import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; -public class RoomMissionLogNotificationVo extends NotificationSendVo { +public class RoomMissionLogNotificationEvent extends NotificationSendEvent { private static final String title = "%s님이 미션을 완료했어요!"; private static final String body = "%s 그룹"; - public RoomMissionLogNotificationVo(User user, Room room, List fcmTokens) { + public RoomMissionLogNotificationEvent(User user, Room room, List fcmTokens) { super(fcmTokens, String.format(title, user.getName()), String.format(body, room.getName()), createDataMap(room.getId())); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java b/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java index dce45c89..1685356b 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java @@ -2,7 +2,7 @@ import com.google.api.core.ApiFuture; import com.google.firebase.messaging.*; -import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendVo; +import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenDeleteEvent; import lombok.RequiredArgsConstructor; @@ -24,11 +24,11 @@ public class FcmService { private final ApplicationEventPublisher eventPublisher; - public void sendMessageToTokens(NotificationSendVo notificationSendVo) { + public void sendMessageToTokens(NotificationSendEvent notificationSendEvent) { - MulticastMessage message = getPreconfiguredMessageToTokens(notificationSendVo); + MulticastMessage message = getPreconfiguredMessageToTokens(notificationSendEvent); ApiFuture response = sendAndGetResponse(message); - deleteInvalidToken(response, notificationSendVo.getFcmTokens().stream() + deleteInvalidToken(response, notificationSendEvent.getFcmTokens().stream() .map(FcmToken::getValue) .collect(Collectors.toList())); } @@ -74,28 +74,28 @@ private ApiFuture sendAndGetResponse(MulticastMessage message) { } - private MulticastMessage getPreconfiguredMessageToTokens(NotificationSendVo notificationSendVo) { - return getPreconfiguredMulticastMessageBuilder(notificationSendVo).addAllTokens(notificationSendVo.getFcmTokens().stream() + private MulticastMessage getPreconfiguredMessageToTokens(NotificationSendEvent notificationSendEvent) { + return getPreconfiguredMulticastMessageBuilder(notificationSendEvent).addAllTokens(notificationSendEvent.getFcmTokens().stream() .map(FcmToken::getValue) .collect(Collectors.toList())) .build(); } - private Message.Builder getPreconfiguredMessageBuilder(NotificationSendVo notificationSendVo) { + private Message.Builder getPreconfiguredMessageBuilder(NotificationSendEvent notificationSendEvent) { return Message.builder() .setNotification(Notification.builder() - .setTitle(notificationSendVo.getTitle()) - .setBody(notificationSendVo.getBody()) + .setTitle(notificationSendEvent.getTitle()) + .setBody(notificationSendEvent.getBody()) .build()); } - private MulticastMessage.Builder getPreconfiguredMulticastMessageBuilder(NotificationSendVo notificationSendVo) { + private MulticastMessage.Builder getPreconfiguredMulticastMessageBuilder(NotificationSendEvent notificationSendEvent) { return MulticastMessage.builder() .setNotification(Notification.builder() - .setTitle(notificationSendVo.getTitle()) - .setBody(notificationSendVo.getBody()) + .setTitle(notificationSendEvent.getTitle()) + .setBody(notificationSendEvent.getBody()) .build()) - .putAllData(notificationSendVo.getDataMap()); + .putAllData(notificationSendEvent.getDataMap()); } } From aa9dfac8fc0e0571d841de61191a9ba7d0113cbd Mon Sep 17 00:00:00 2001 From: hye-on Date: Mon, 22 Apr 2024 13:26:55 +0900 Subject: [PATCH 7/7] =?UTF-8?q?#123=20[FIX]=20:=EB=A6=AC=EB=B7=B0=EB=B0=98?= =?UTF-8?q?=EC=98=81.=20EventPublisher=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../togetUp/api/event/EventPublisher.java | 19 +++++++++++++++++++ .../NotificationEventPublisher.java | 17 ----------------- .../api/notification/NotificationService.java | 6 +++--- .../togetUp/config/EventsConfig.java | 19 +++++++++++++++++++ .../togetUp/infra/fcm/FcmService.java | 4 ++-- 5 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/event/EventPublisher.java delete mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/config/EventsConfig.java diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/event/EventPublisher.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/event/EventPublisher.java new file mode 100644 index 00000000..85aed68b --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/event/EventPublisher.java @@ -0,0 +1,19 @@ +package com.wakeUpTogetUp.togetUp.api.event; + +import org.springframework.context.ApplicationEventPublisher; + +public class EventPublisher { + + private static ApplicationEventPublisher publisher; + + public static void setPublisher(ApplicationEventPublisher publisher) { + EventPublisher.publisher = publisher; + } + + public static void raise(Object event) { + if (publisher != null) { + publisher.publishEvent(event); + } + } + +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java deleted file mode 100644 index fd9ae17f..00000000 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationEventPublisher.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.wakeUpTogetUp.togetUp.api.notification; - -import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent; -import lombok.RequiredArgsConstructor; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.stereotype.Component; - -@Component -@RequiredArgsConstructor -public class NotificationEventPublisher { - private final ApplicationEventPublisher eventPublisher; - - public void publishNotificationSendEvent(NotificationSendEvent notificationSendEvent) { - eventPublisher.publishEvent(notificationSendEvent); - } -} - diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java index b099c08f..405cd5ee 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java @@ -1,5 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.notification; +import com.wakeUpTogetUp.togetUp.api.event.EventPublisher; import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent; import com.wakeUpTogetUp.togetUp.api.notification.vo.RoomMissionLogNotificationEvent; import com.wakeUpTogetUp.togetUp.api.room.RoomRepository; @@ -28,10 +29,9 @@ public class NotificationService { private final RoomRepository roomRepository; private final UserRepository userRepository; private final FcmTokenRepository fcmTokenRepository; - private final NotificationEventPublisher notificationEventPublisher; public void sendNotificationToAllUsers(String title, String body) { - notificationEventPublisher.publishNotificationSendEvent(new NotificationSendEvent( + EventPublisher.raise(new NotificationSendEvent( fcmTokenRepository.findAllByUser_IdIn(userService.getAgreedNotiUsersIds()), title, body, @@ -53,7 +53,7 @@ public void sendNotificationToUsersInRoom(Integer alarmId, Integer missionComple .collect(Collectors.toList()); if (userIdsInRoom.size() > 0) { - notificationEventPublisher.publishNotificationSendEvent(new RoomMissionLogNotificationEvent( + EventPublisher.raise(new RoomMissionLogNotificationEvent( user, room, fcmTokenRepository.findAllByUser_IdIn(userIdsInRoom) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/config/EventsConfig.java b/src/main/java/com/wakeUpTogetUp/togetUp/config/EventsConfig.java new file mode 100644 index 00000000..ad3d0a3d --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/config/EventsConfig.java @@ -0,0 +1,19 @@ +package com.wakeUpTogetUp.togetUp.config; + +import com.wakeUpTogetUp.togetUp.api.event.EventPublisher; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +@Configuration +public class EventsConfig { + @Autowired + private ApplicationContext applicationContext; + + @Bean + public InitializingBean eventsInitializer(ApplicationEventPublisher eventPublisher) { + return () -> EventPublisher.setPublisher(eventPublisher); + } +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java b/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java index 1685356b..49e273cf 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/infra/fcm/FcmService.java @@ -2,6 +2,7 @@ import com.google.api.core.ApiFuture; import com.google.firebase.messaging.*; +import com.wakeUpTogetUp.togetUp.api.event.EventPublisher; import com.wakeUpTogetUp.togetUp.api.notification.vo.NotificationSendEvent; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenDeleteEvent; @@ -21,7 +22,6 @@ @RequiredArgsConstructor public class FcmService { private final FirebaseMessaging firebaseMessaging; - private final ApplicationEventPublisher eventPublisher; public void sendMessageToTokens(NotificationSendEvent notificationSendEvent) { @@ -59,7 +59,7 @@ public void deleteInvalidToken(ApiFuture response, List d }); - eventPublisher.publishEvent(new FcmTokenDeleteEvent(failedDeviceTokens)); + EventPublisher.raise(new FcmTokenDeleteEvent(failedDeviceTokens)); } } catch (InterruptedException e) { throw new RuntimeException(e);