From 9cfb184920d8bee972ffd4ffce4beaf18be8121a Mon Sep 17 00:00:00 2001 From: kimyu0218 Date: Sun, 8 Dec 2024 22:29:05 +0900 Subject: [PATCH] =?UTF-8?q?test:=20notification=20+=20data=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20api=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../firebase/PushMessageSenderImpl.java | 15 +++++++++++++++ .../application/firebase/TopicGenerator.java | 4 ++++ .../firebase/PushMessageSender.java | 2 ++ .../presentation/fcmtest/FcmTestController.java | 16 ++++++++++++++++ 4 files changed, 37 insertions(+) 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(); + } }