diff --git a/src/main/java/com/twentythree/peech/fcm/application/NotificationService.java b/src/main/java/com/twentythree/peech/fcm/application/NotificationService.java index 57396d6..0360100 100644 --- a/src/main/java/com/twentythree/peech/fcm/application/NotificationService.java +++ b/src/main/java/com/twentythree/peech/fcm/application/NotificationService.java @@ -5,4 +5,5 @@ public interface NotificationService { void pushNotification(Long userId); void saveOrUpdateToken(RequestFCMTokenDTO fcmTokenDTO, Long userId); + void testPushNotification(Long userId); } diff --git a/src/main/java/com/twentythree/peech/fcm/application/NotificationServiceImpl.java b/src/main/java/com/twentythree/peech/fcm/application/NotificationServiceImpl.java index 4a645af..163ca5b 100644 --- a/src/main/java/com/twentythree/peech/fcm/application/NotificationServiceImpl.java +++ b/src/main/java/com/twentythree/peech/fcm/application/NotificationServiceImpl.java @@ -4,11 +4,11 @@ import com.twentythree.peech.fcm.dto.request.RequestFCMTokenDTO; import com.twentythree.peech.fcm.entity.NotificationEntity; import com.twentythree.peech.fcm.event.FCMPushedEvent; +import com.twentythree.peech.fcm.event.FCMTestPushEvent; import com.twentythree.peech.fcm.infra.NotificationRepository; import com.twentythree.peech.user.entity.UserEntity; import com.twentythree.peech.user.repository.UserRepository; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -53,6 +53,17 @@ public void saveOrUpdateToken(RequestFCMTokenDTO request, Long userId) { }); } + @Override + public void testPushNotification(Long userId) { + List fcmTokenList = notificationRepository.findAllByUserId(userId); + + if(fcmTokenList.isEmpty()){ + throw new IllegalStateException("FCM 토큰이 존재하지 않습니다."); + } + + applicationEventPublisher.publishEvent(new FCMTestPushEvent(fcmTokenList)); + } + private void updateFCMToken(NotificationEntity originEntity, String newToken){ originEntity.updateToken(newToken); } diff --git a/src/main/java/com/twentythree/peech/fcm/event/FCMTestPushEvent.java b/src/main/java/com/twentythree/peech/fcm/event/FCMTestPushEvent.java new file mode 100644 index 0000000..e5b63c2 --- /dev/null +++ b/src/main/java/com/twentythree/peech/fcm/event/FCMTestPushEvent.java @@ -0,0 +1,12 @@ +package com.twentythree.peech.fcm.event; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.List; + +@Getter +@AllArgsConstructor +public class FCMTestPushEvent { + private List fcmTokenList; +} diff --git a/src/main/java/com/twentythree/peech/fcm/event/handler/FCMEventHandler.java b/src/main/java/com/twentythree/peech/fcm/event/handler/FCMEventHandler.java index 15e2786..79a8884 100644 --- a/src/main/java/com/twentythree/peech/fcm/event/handler/FCMEventHandler.java +++ b/src/main/java/com/twentythree/peech/fcm/event/handler/FCMEventHandler.java @@ -2,8 +2,8 @@ import com.google.firebase.ErrorCode; import com.google.firebase.messaging.*; -import com.twentythree.peech.fcm.application.NotificationService; import com.twentythree.peech.fcm.event.FCMPushedEvent; +import com.twentythree.peech.fcm.event.FCMTestPushEvent; import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,4 +42,25 @@ public void sendMessage(FCMPushedEvent fcmEventDTO) throws FirebaseMessagingExce } + @EventListener + public void testPush(FCMTestPushEvent fcmTokenList) throws FirebaseMessagingException { + + MulticastMessage message = MulticastMessage.builder() + .putData("title", "테스트 푸시 알림") + .putData("body", "테스트 푸시 알림을 보냅니다.") + .addAllTokens(fcmTokenList.getFcmTokenList()) + .build(); + + BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message); + log.info("test messsage 전송완료"); + + //Todo: 실패한 푸시 알림에 대한 처리 + response.getResponses().stream() + .filter(res -> !res.isSuccessful()) + .filter(res -> res.getException().getErrorCode() == ErrorCode.INVALID_ARGUMENT) + .forEach(res -> { + log.error("Failed to send message to: id = {} {}", res.getMessageId(), res.getException().getMessage()); + }); + } + } diff --git a/src/main/java/com/twentythree/peech/fcm/presentation/FCMController.java b/src/main/java/com/twentythree/peech/fcm/presentation/FCMController.java index 80b887a..7b98ab1 100644 --- a/src/main/java/com/twentythree/peech/fcm/presentation/FCMController.java +++ b/src/main/java/com/twentythree/peech/fcm/presentation/FCMController.java @@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RestController; @@ -25,4 +26,12 @@ public ResponseEntity putFCMToken(RequestFCMTokenDTO request, @Authenticat return ResponseEntity.ok().build(); } + + @GetMapping("/api/v2/notification/test") + public ResponseEntity testNotification(@AuthenticationPrincipal JWTAuthentication jwtAuthentication){ + Long userId = jwtAuthentication.getUserId(); + notificationService.testPushNotification(userId); + + return ResponseEntity.ok("요청 처리 완료"); + } }