diff --git a/src/main/java/com/server/capple/config/apns/dto/ApnsClientRequest.java b/src/main/java/com/server/capple/config/apns/dto/ApnsClientRequest.java index a7708140..86da0670 100644 --- a/src/main/java/com/server/capple/config/apns/dto/ApnsClientRequest.java +++ b/src/main/java/com/server/capple/config/apns/dto/ApnsClientRequest.java @@ -1,11 +1,13 @@ package com.server.capple.config.apns.dto; import com.fasterxml.jackson.annotation.JsonProperty; +import com.server.capple.domain.board.entity.Board; +import com.server.capple.domain.boardComment.entity.BoardComment; +import com.server.capple.domain.notifiaction.entity.NotificationType; import io.swagger.v3.oas.annotations.media.Schema; import lombok.*; public class ApnsClientRequest { - @Getter @NoArgsConstructor @ToString @@ -33,7 +35,6 @@ public static class Aps { private String threadId; @JsonProperty("target-content-id") private String targetContentId; // 프론트 측 작업 필요함 - @ToString @Getter @AllArgsConstructor @@ -47,53 +48,90 @@ public static class Alert { } @Getter - @AllArgsConstructor @NoArgsConstructor - @Builder @ToString - public static class FullAlertBody { + public static class BoardNotificationBody { private Aps aps; + private String boardId; + @Builder + public BoardNotificationBody(NotificationType type, Board board) { + this.aps = Aps.builder() + .threadId("board-" + board.getId()) + .alert(Aps.Alert.builder() + .title(type.getTitle()) + .body(board.getContent()) + .build()) + .build(); + this.boardId = board.getId().toString(); + } + @ToString @Getter + @Builder @AllArgsConstructor @NoArgsConstructor + public static class Aps { + private Alert alert; + @Builder.Default + private String sound = "default"; // Library/Sounds 폴더 내의 파일 이름 + @JsonProperty("thread-id") + private String threadId; + + @ToString + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class Alert { + private String title; + private String body; + } + } + } + + @Getter + @NoArgsConstructor + @ToString + public static class BoardCommentNotificationBody { + private Aps aps; + private String boardId; + private String boardCommentId; @Builder + public BoardCommentNotificationBody(NotificationType type, Board board, BoardComment boardComment) { + this.aps = Aps.builder().threadId("board-" + board.getId()) + .alert(Aps.Alert.builder() + .title(type.getTitle()) + .subtitle(boardComment.getContent()) + .body(board.getContent()) + .build()) + .build(); + this.boardId = board.getId().toString(); + this.boardCommentId = boardComment.getId().toString(); + } + @ToString + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor public static class Aps { - private Alert alert; // alert 정보 - private Integer badge; // 앱 아이콘에 표시할 뱃지 숫자 + private Alert alert; + private Integer badge; @Schema(defaultValue = "default") private String sound; // Library/Sounds 폴더 내의 파일 이름 @JsonProperty("thread-id") - private String threadId; // 알림 그룹화를 위한 thread id (UNNotificationContent 객체의 threadIdentifier와 일치해야 함) - private String category; // 알림 그룹화를 위한 category, (UNNotificationCategory 식별자와 일치해야 함) - @Schema(defaultValue = "0") - @JsonProperty("content-available") - private Integer contentAvailable; // 백그라운드 알림 여부, 1이면 백그라운드 알림, 0이면 포그라운드 알림 (백그라운드일 경우 alert, badge, sound는 넣으면 안됨) - @Schema(defaultValue = "0") - @JsonProperty("mutable-content") - private Integer mutableContent; // 알림 서비스 확장 플래그 - @Schema(defaultValue = "") - @JsonProperty("target-content-id") - private String targetContentId; // 알림이 클릭되었을 때 가져올 창의 식별자, UNNotificationContent 객체에 채워짐 + private String threadId; + @ToString @Getter + @Builder @AllArgsConstructor @NoArgsConstructor - @Builder - @ToString public static class Alert { - @Schema(defaultValue = "title") private String title; - @Schema(defaultValue = "subTitle") private String subtitle; - @Schema(defaultValue = "body") private String body; - @Schema(defaultValue = "") - @JsonProperty("launch-image") - private String launchImage; // 실행시 보여줄 이미지 파일, 기본 실행 이미지 대신 입력한 이미지 또는 스토리보드가 켜짐 } } } - } diff --git a/src/main/java/com/server/capple/domain/notifiaction/service/NotificationServiceImpl.java b/src/main/java/com/server/capple/domain/notifiaction/service/NotificationServiceImpl.java index d6091318..a01bbe1b 100644 --- a/src/main/java/com/server/capple/domain/notifiaction/service/NotificationServiceImpl.java +++ b/src/main/java/com/server/capple/domain/notifiaction/service/NotificationServiceImpl.java @@ -22,12 +22,9 @@ public class NotificationServiceImpl implements NotificationService { @Override public void sendBoardHeartNotification(Long actorId, Board board) { if (actorId.equals(board.getWriter().getId())) return; - apnsService.sendApnsToMembers(ApnsClientRequest.SimplePushBody.builder() - .title(BOARD_HEART.getTitle()) - .body(board.getContent()) - .threadId("board-" + board.getId()) - .sound("default") - .boardId(board.getId().toString()) + apnsService.sendApnsToMembers(ApnsClientRequest.BoardNotificationBody.builder() + .type(BOARD_HEART) + .board(board) .build(), board.getWriter().getId()); // TODO 알림 데이터베이스 저장 } @@ -41,42 +38,29 @@ public void sendBoardCommentNotification(Long actorId, Board board, BoardComment // 게시판 구독자에게 알림 전송 .peek(subscriberId -> { if (subscriberId.equals(board.getWriter().getId())) { - apnsService.sendApnsToMembers(ApnsClientRequest.SimplePushBody.builder() - .title(BOARD_COMMENT.getTitle()) - .subTitle(boardComment.getContent()) - .body(board.getContent()) - .threadId("board-" + board.getId()) - .sound("default") - .boardId(board.getId().toString()) - .boardCommentId(boardComment.getId().toString()) + apnsService.sendApnsToMembers(ApnsClientRequest.BoardCommentNotificationBody.builder() + .type(BOARD_COMMENT) + .board(board) + .boardComment(boardComment) .build(), subscriberId); } }) .filter(id -> !id.equals(board.getWriter().getId())) .toList(); - ApnsClientRequest.SimplePushBody simplePushBody = ApnsClientRequest.SimplePushBody.builder() - .title(BAORD_COMMENT_DUPLCATE.getTitle()) - .subTitle(boardComment.getContent()) - .body(board.getContent()) - .threadId("board-" + board.getId()) - .sound("default") - .boardId(board.getId().toString()) - .boardCommentId(boardComment.getId().toString()) - .build(); - apnsService.sendApnsToMembers(simplePushBody, subscriberIds); + apnsService.sendApnsToMembers(ApnsClientRequest.BoardCommentNotificationBody.builder() + .type(BAORD_COMMENT_DUPLCATE) + .board(board) + .boardComment(boardComment) + .build(), subscriberIds); // TODO 알림 데이터베이스 저장 } @Override public void sendBoardCommentHeartNotification(Long actorId, Board board, BoardComment boardComment) { - apnsService.sendApnsToMembers(ApnsClientRequest.SimplePushBody.builder() - .title(BOARD_COMMENT_HEART.getTitle()) - .subTitle(boardComment.getContent()) - .body(board.getContent()) - .threadId("board-" + board.getId()) - .sound("default") - .boardId(board.getId().toString()) - .boardCommentId(boardComment.getId().toString()) + apnsService.sendApnsToMembers(ApnsClientRequest.BoardCommentNotificationBody.builder() + .type(BOARD_COMMENT_HEART) + .board(board) + .boardComment(boardComment) .build(), boardComment.getMember().getId()); // TODO 알림 데이터베이스 저장 } diff --git a/src/test/java/com/server/capple/config/apns/service/ApnsServiceImplTest.java b/src/test/java/com/server/capple/config/apns/service/ApnsServiceImplTest.java index 266b30d4..e4e9305a 100644 --- a/src/test/java/com/server/capple/config/apns/service/ApnsServiceImplTest.java +++ b/src/test/java/com/server/capple/config/apns/service/ApnsServiceImplTest.java @@ -1,6 +1,6 @@ package com.server.capple.config.apns.service; -import com.server.capple.config.apns.dto.ApnsClientRequest.SimplePushBody; +import com.server.capple.config.apns.dto.ApnsClientRequest; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -34,7 +34,7 @@ void sendApns() { String targetContentId = "targetContentId"; //when - Boolean result = apnsService.sendApns(SimplePushBody.builder().title(title).subTitle(subTitle).body(body).sound("default").threadId(threadId).targetContentId(targetContentId).build(), List.of(simulatorDeviceToken)); + Boolean result = apnsService.sendApns(ApnsClientRequest.SimplePushBody.builder().title(title).subTitle(subTitle).body(body).sound("default").threadId(threadId).targetContentId(targetContentId).build(), List.of(simulatorDeviceToken)); //then assertTrue(result); @@ -55,7 +55,7 @@ void sendApnsMessages() { for (int i = 0; i < 100; i++) deviceTokens.add(simulatorDeviceToken); //when - Boolean result = apnsService.sendApns(SimplePushBody.builder().title(title).subTitle(subTitle).body(body).sound("default").threadId(threadId).targetContentId(targetContentId).build(), deviceTokens); + Boolean result = apnsService.sendApns(ApnsClientRequest.SimplePushBody.builder().title(title).subTitle(subTitle).body(body).sound("default").threadId(threadId).targetContentId(targetContentId).build(), deviceTokens); //then assertTrue(result);