From e7ac592c605ebc4d8a34d175a466e4e2e4d6d0c2 Mon Sep 17 00:00:00 2001 From: sonshn Date: Sat, 2 Mar 2024 17:40:32 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]:=20Alarm=20API:=20FCM=20Token=20=EC=97=B0?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alarm/controller/AlarmController.java | 9 ++++--- .../domain/alarm/dto/FCMMessageDto.java | 25 +++++++++++++++++++ .../domain/alarm/dto/FCMMessageTestDto.java | 5 +--- .../domain/alarm/service/FCMService.java | 22 +++++++++------- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/umc/commonplant/domain/alarm/controller/AlarmController.java b/src/main/java/com/umc/commonplant/domain/alarm/controller/AlarmController.java index 5ac1cea..018484b 100644 --- a/src/main/java/com/umc/commonplant/domain/alarm/controller/AlarmController.java +++ b/src/main/java/com/umc/commonplant/domain/alarm/controller/AlarmController.java @@ -22,13 +22,14 @@ public class AlarmController { @PostMapping("/api/fcm") public ResponseEntity pushMessage(@RequestBody FCMMessageTestDto fcmTestDto) throws IOException { - // log.info(fcmTestDto.getTargetToken() + " " +fcmTestDto.getTitle() + " " + fcmTestDto.getBody()); - log.info(fcmTestDto.getTitle() + " " + fcmTestDto.getBody()); + log.info(fcmTestDto.getTargetToken() + " " +fcmTestDto.getTitle() + " " + fcmTestDto.getBody()); + // log.info(fcmTestDto.getTitle() + " " + fcmTestDto.getBody()); fcmService.sendMessageTo( - //fcmTestDto.getTargetToken(), + fcmTestDto.getTargetToken(), fcmTestDto.getTitle(), - fcmTestDto.getBody()); + fcmTestDto.getBody() + ); return ResponseEntity.ok().build(); } diff --git a/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageDto.java b/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageDto.java index e067540..2715810 100644 --- a/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageDto.java +++ b/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageDto.java @@ -28,6 +28,7 @@ public static class Message { private Notification notification; private String token; private Data data; + private Apns apns; } /** @@ -55,4 +56,28 @@ public static class Data { private String description; } + /** + * iOS 알림 추가 설정: 소리 등등 + */ + @Builder + @AllArgsConstructor + @Getter + public static class Aps { + private String sound; + } + + @Builder + @AllArgsConstructor + @Getter + public static class Payload { + private Aps aps; + } + + @Builder + @AllArgsConstructor + @Getter + public static class Apns { + private Payload payload; + } + } diff --git a/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageTestDto.java b/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageTestDto.java index 5167111..676eaa1 100644 --- a/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageTestDto.java +++ b/src/main/java/com/umc/commonplant/domain/alarm/dto/FCMMessageTestDto.java @@ -11,9 +11,6 @@ public class FCMMessageTestDto { private String title; private String body; - // private String targetToken; + private String targetToken; - public FCMMessageTestDto(){ - - } } diff --git a/src/main/java/com/umc/commonplant/domain/alarm/service/FCMService.java b/src/main/java/com/umc/commonplant/domain/alarm/service/FCMService.java index 47aeeef..5c5901b 100644 --- a/src/main/java/com/umc/commonplant/domain/alarm/service/FCMService.java +++ b/src/main/java/com/umc/commonplant/domain/alarm/service/FCMService.java @@ -55,19 +55,23 @@ private String getAccessToken() throws IOException { * @return * @throws JsonProcessingException */ - // private String makeMessage(String targetToken, String title, String body) throws JsonProcessingException { - private String makeMessage(String title, String body) throws JsonProcessingException { + private String makeMessage(String targetToken, String title, String body) throws JsonProcessingException { + // private String makeMessage(String title, String body) throws JsonProcessingException { FCMMessageDto fcmMessage = FCMMessageDto.builder() .message(FCMMessageDto.Message.builder() - //.token(targetToken) + .token(targetToken) .notification(FCMMessageDto.Notification.builder() .title(title) .body(body) .image(null) .build() ) - .build() - ) + .apns(FCMMessageDto.Apns.builder() + .payload(FCMMessageDto.Payload.builder() + .aps(FCMMessageDto.Aps.builder().sound("default").build()) + .build()) + .build()) + .build()) .validate_only(false) .build(); @@ -81,10 +85,10 @@ private String makeMessage(String title, String body) throws JsonProcessingExcep * @param body * @throws IOException */ - // public void sendMessageTo(String targetToken, String title, String body) throws IOException { - public void sendMessageTo(String title, String body) throws IOException { - // String message = makeMessage(targetToken, title, body); - String message = makeMessage(title, body); + public void sendMessageTo(String targetToken, String title, String body) throws IOException { + // public void sendMessageTo(String title, String body) throws IOException { + String message = makeMessage(targetToken, title, body); + // String message = makeMessage(title, body); OkHttpClient client = new OkHttpClient();