diff --git a/src/main/java/com/nexters/goalpanzi/application/firebase/PushMessageSenderImpl.java b/src/main/java/com/nexters/goalpanzi/application/firebase/PushMessageSenderImpl.java index 85ca4c92..6b8a8f2c 100644 --- a/src/main/java/com/nexters/goalpanzi/application/firebase/PushMessageSenderImpl.java +++ b/src/main/java/com/nexters/goalpanzi/application/firebase/PushMessageSenderImpl.java @@ -79,4 +79,19 @@ public void sendGroupData(final String title, final String body, final String to private Data makeData(final String title, final String body, final Long missionId) { return new Data(title, body, missionId); } + + public void sendNotificationWithData(String title, String body, String topic) { + Notification notification = makeNotification(title, body); + Message message = Message.builder() + .setNotification(notification) + .putData("missionId", TopicGenerator.extractIdentifier(topic)) + .setTopic(topic) + .build(); + + try { + FirebaseMessaging.getInstance().send(message); + } catch (FirebaseMessagingException e) { + throw new BaseException(ErrorCode.FAILED_TO_SEND_GROUP_MESSAGE, e); + } + } } diff --git a/src/main/java/com/nexters/goalpanzi/application/firebase/TopicGenerator.java b/src/main/java/com/nexters/goalpanzi/application/firebase/TopicGenerator.java index a8cb273c..cb03e69b 100644 --- a/src/main/java/com/nexters/goalpanzi/application/firebase/TopicGenerator.java +++ b/src/main/java/com/nexters/goalpanzi/application/firebase/TopicGenerator.java @@ -7,4 +7,8 @@ public class TopicGenerator { public static String getTopic(Long missionId) { return TOPIC_PREFIX + missionId; } + + public static String extractIdentifier(String topic) { + return topic.replace(TOPIC_PREFIX, ""); + } } diff --git a/src/main/java/com/nexters/goalpanzi/infrastructure/firebase/PushMessageSender.java b/src/main/java/com/nexters/goalpanzi/infrastructure/firebase/PushMessageSender.java index a0b73fea..0e9a20c2 100644 --- a/src/main/java/com/nexters/goalpanzi/infrastructure/firebase/PushMessageSender.java +++ b/src/main/java/com/nexters/goalpanzi/infrastructure/firebase/PushMessageSender.java @@ -9,4 +9,6 @@ public interface PushMessageSender { void sendIndividualData(final String title, final String body, final String token, final Long missionId); void sendGroupData(final String title, final String body, final String topic, final Long missionId); + + void sendNotificationWithData(final String title, final String body, final String topic); } diff --git a/src/main/java/com/nexters/goalpanzi/presentation/fcmtest/FcmTestController.java b/src/main/java/com/nexters/goalpanzi/presentation/fcmtest/FcmTestController.java index 2e0c606b..e50e752b 100644 --- a/src/main/java/com/nexters/goalpanzi/presentation/fcmtest/FcmTestController.java +++ b/src/main/java/com/nexters/goalpanzi/presentation/fcmtest/FcmTestController.java @@ -1,5 +1,6 @@ package com.nexters.goalpanzi.presentation.fcmtest; +import com.nexters.goalpanzi.application.firebase.TopicGenerator; import com.nexters.goalpanzi.infrastructure.firebase.PushMessageSender; import com.nexters.goalpanzi.infrastructure.firebase.TopicSubscriber; import io.swagger.v3.oas.annotations.Operation; @@ -57,4 +58,19 @@ ResponseEntity sendData( return ResponseEntity.ok().build(); } + + @Operation + @GetMapping("notification-with-data") + ResponseEntity sendNotificationWithData( + @Schema(description = "deviceToken", requiredMode = Schema.RequiredMode.REQUIRED) + @RequestParam final String deviceToken + ) { + pushMessageSender.sendNotificationWithData( + "혼합 메시지 테스트", + "notification { title: string, body: string }, data { missionId: string }", + TopicGenerator.getTopic(1L) + ); + + return ResponseEntity.ok().build(); + } }