From 32e088b9f85c531896620b57050e70fedc05080e Mon Sep 17 00:00:00 2001 From: JaeSeo Yang <96044622+psychology50@users.noreply.github.com> Date: Wed, 28 Feb 2024 02:43:12 +0900 Subject: [PATCH] =?UTF-8?q?test:=20#39=20push=20notification=20test=20api?= =?UTF-8?q?=20=EA=B0=9C=EB=B0=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fitapet-app-external-api/.gitignore | 5 +- .../apis/test/NotificationTestController.java | 25 +++++++ .../apis/test/NotificationTestService.java | 67 +++++++++++++++++++ .../fcm/FcmNotificationServiceImpl.java | 10 +-- .../infra/client/fcm/NotificationService.java | 10 +-- .../event/FcmNotificationEventHandler.java | 24 +------ 6 files changed, 109 insertions(+), 32 deletions(-) create mode 100644 fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestController.java create mode 100644 fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestService.java diff --git a/fitapet-app-external-api/.gitignore b/fitapet-app-external-api/.gitignore index b63da455..b1e627d7 100644 --- a/fitapet-app-external-api/.gitignore +++ b/fitapet-app-external-api/.gitignore @@ -39,4 +39,7 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +## TEST API ## +## **/test \ No newline at end of file diff --git a/fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestController.java b/fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestController.java new file mode 100644 index 00000000..83d8d015 --- /dev/null +++ b/fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestController.java @@ -0,0 +1,25 @@ +package kr.co.fitapet.api.apis.test; + +import io.swagger.v3.oas.annotations.tags.Tag; +import kr.co.fitapet.api.common.security.authentication.CustomUserDetails; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "푸시 테스트 API", description = "push notification을 위한 개발자용 임시 API 입니다. 테스트 후 삭제 예정") +@RestController +@RequiredArgsConstructor +@Slf4j +@RequestMapping("/api/v2/test/members") +public class NotificationTestController { + private final NotificationTestService notificationTestService; + + @RequestMapping("/push") + @PreAuthorize("isAuthenticated()") + public void push(@AuthenticationPrincipal CustomUserDetails user) { + notificationTestService.push(user.getUserId()); + } +} diff --git a/fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestService.java b/fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestService.java new file mode 100644 index 00000000..58487593 --- /dev/null +++ b/fitapet-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestService.java @@ -0,0 +1,67 @@ +package kr.co.fitapet.api.apis.test; + +import kr.co.fitapet.domain.domains.device.domain.DeviceToken; +import kr.co.fitapet.domain.domains.device.service.DeviceTokenSearchService; +import kr.co.fitapet.infra.client.fcm.NotificationDataKey; +import kr.co.fitapet.infra.client.fcm.NotificationService; +import kr.co.fitapet.infra.client.fcm.request.NotificationRequest; +import kr.co.fitapet.infra.client.fcm.request.NotificationSingleRequest; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Slf4j +@Service +@RequiredArgsConstructor +public class NotificationTestService { + private final DeviceTokenSearchService deviceTokenSearchService; + private final NotificationService notificationService; + + public void push(Long userId) { + log.info("push to {}", userId); + + List deviceTokens = deviceTokenSearchService.findDeviceTokensByMemberId(userId); + log.info("deviceTokens: {}", deviceTokens); + + log.info("send push message"); + deviceTokens.forEach(deviceToken -> { + NotificationSingleRequest request = NotificationSingleRequest.builder() + .token(deviceToken.getDeviceToken()) + .title("푸시 테스트") + .content("푸시 테스트 메시지") + .build(); + + notificationService.sendMessage(request); + }); + + log.info("send push message with image"); + deviceTokens.forEach(deviceToken -> { + NotificationSingleRequest request = NotificationSingleRequest.builder() + .title("이미지 테스트") + .content("푸시 이미지 테스트 메시지") + .imageUrl("https://pkcy.kr.object.ncloudstorage.com/profile/0bb25dde9020.jpeg") + .build(); + + notificationService.sendMessage(request); + }); + + log.info("send push message with data"); + deviceTokens.forEach(deviceToken -> { + NotificationSingleRequest request = NotificationSingleRequest.builder() + .title("데이터 테스트") + .content("푸시 데이터 테스트 메시지") + .data( + Map.of( + NotificationDataKey.NOTICE_TYPE.getField(), "TEST_TYPE", + NotificationDataKey.TO_ID.getField(), userId.toString() + ) + ) + .build(); + + notificationService.sendMessage(request); + }); + } +} diff --git a/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/FcmNotificationServiceImpl.java b/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/FcmNotificationServiceImpl.java index 01a18d67..ae6f7f3b 100644 --- a/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/FcmNotificationServiceImpl.java +++ b/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/FcmNotificationServiceImpl.java @@ -19,7 +19,7 @@ public class FcmNotificationServiceImpl implements NotificationService { private final FirebaseMessaging firebaseMessaging; @Override - public void sendMessage(NotificationSingleRequest request) throws FirebaseMessagingException { + public void sendMessage(NotificationSingleRequest request) { Message message = request.buildSendMessage().setApnsConfig(getApnsConfigToTopic(request)).build(); ApiFuture response = firebaseMessaging.sendAsync(message); @@ -27,7 +27,7 @@ public void sendMessage(NotificationSingleRequest request) throws FirebaseMessag } @Override - public void sendMessages(NotificationMulticastRequest request) throws FirebaseMessagingException { + public void sendMessages(NotificationMulticastRequest request) { MulticastMessage messages = request.buildSendMessage().setApnsConfig(getApnsConfigToTopic(request)).build(); ApiFuture response = firebaseMessaging.sendEachForMulticastAsync(messages); @@ -35,7 +35,7 @@ public void sendMessages(NotificationMulticastRequest request) throws FirebaseMe } @Override - public void sendMessagesToTopic(NotificationTopicRequest request) throws FirebaseMessagingException { + public void sendMessagesToTopic(NotificationTopicRequest request) { Message message = request.buildSendMessage().setApnsConfig(getApnsConfigToTopic(request)).build(); ApiFuture response = firebaseMessaging.sendAsync(message); @@ -43,12 +43,12 @@ public void sendMessagesToTopic(NotificationTopicRequest request) throws Firebas } @Override - public void subScribe(String topic, List memberTokens) throws FirebaseMessagingException { + public void subScribe(String topic, List memberTokens) { firebaseMessaging.subscribeToTopicAsync(memberTokens, topic); } @Override - public void unSubScribe(String topic, List memberTokens) throws FirebaseMessagingException { + public void unSubScribe(String topic, List memberTokens) { firebaseMessaging.unsubscribeFromTopicAsync(memberTokens, topic); } diff --git a/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/NotificationService.java b/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/NotificationService.java index 2d6a4431..abbb5877 100644 --- a/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/NotificationService.java +++ b/fitapet-infra/src/main/java/kr/co/fitapet/infra/client/fcm/NotificationService.java @@ -8,9 +8,9 @@ import java.util.List; public interface NotificationService { - void sendMessage(NotificationSingleRequest request) throws FirebaseMessagingException; - void sendMessages(NotificationMulticastRequest request) throws FirebaseMessagingException; - void sendMessagesToTopic(NotificationTopicRequest request) throws FirebaseMessagingException; - void subScribe(String topic, List memberTokens) throws FirebaseMessagingException; - void unSubScribe(String topic, List memberTokens) throws FirebaseMessagingException; + void sendMessage(NotificationSingleRequest request); + void sendMessages(NotificationMulticastRequest request); + void sendMessagesToTopic(NotificationTopicRequest request); + void subScribe(String topic, List memberTokens); + void unSubScribe(String topic, List memberTokens); } diff --git a/fitapet-infra/src/main/java/kr/co/fitapet/infra/common/event/FcmNotificationEventHandler.java b/fitapet-infra/src/main/java/kr/co/fitapet/infra/common/event/FcmNotificationEventHandler.java index f46e4d97..082e397d 100644 --- a/fitapet-infra/src/main/java/kr/co/fitapet/infra/common/event/FcmNotificationEventHandler.java +++ b/fitapet-infra/src/main/java/kr/co/fitapet/infra/common/event/FcmNotificationEventHandler.java @@ -31,13 +31,7 @@ public void handleTokenEvent(NotificationSingleEvent event) { log.info("handleTokensEvent: {}", event); NotificationSingleRequest request = NotificationSingleRequest.fromEvent(event); - try { - notificationService.sendMessage(request); - } catch (FirebaseException e) { - log.error("Failed to send FCM message", e); - e.printStackTrace(); - return; - } + notificationService.sendMessage(request); log.info("Successfully sent FCM message"); } @@ -57,13 +51,7 @@ public void handleTokensEvent(NotificationMulticastEvent event) { log.info("handleTokensEvent: {}", event); NotificationMulticastRequest request = NotificationMulticastRequest.fromEvent(event); - try { - notificationService.sendMessages(request); - } catch (FirebaseException e) { - log.error("Failed to send FCM message", e); - e.printStackTrace(); - return; - } + notificationService.sendMessages(request); log.info("Successfully sent FCM message"); } @@ -83,13 +71,7 @@ public void handleTopicEvent(NotificationTopicEvent event) { log.info("handleTopicEvent: {}", event); NotificationTopicRequest request = NotificationTopicRequest.fromEvent(event); - try { - notificationService.sendMessagesToTopic(request); - } catch (FirebaseException e) { - log.error("Failed to send FCM message", e); - e.printStackTrace(); - return; - } + notificationService.sendMessagesToTopic(request); log.info("Successfully sent FCM message"); }