-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from KCY-Fit-a-Pet/feat/39
๐ง Push Notification Test API
- Loading branch information
Showing
37 changed files
with
1,193 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,7 @@ bin/ | |
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store | ||
.DS_Store | ||
|
||
## TEST API ## | ||
## **/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...pet-app-external-api/src/main/java/kr/co/fitapet/api/apis/profile/dto/DeviceTokenReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package kr.co.fitapet.api.apis.profile.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import kr.co.fitapet.domain.domains.device.domain.DeviceToken; | ||
import kr.co.fitapet.domain.domains.member.domain.Member; | ||
|
||
@Schema(description = "๋๋ฐ์ด์ค ํ ํฐ ๋ฑ๋ก ์์ฒญ") | ||
public record DeviceTokenReq( | ||
@Schema(description = "๋๋ฐ์ด์ค ํ ํฐ", requiredMode = Schema.RequiredMode.REQUIRED) | ||
@NotBlank | ||
String deviceToken, | ||
@Schema(description = "๋๋ฐ์ด์ค OS", requiredMode = Schema.RequiredMode.REQUIRED) | ||
@NotBlank | ||
String os, | ||
@Schema(description = "๋๋ฐ์ด์ค ๋ชจ๋ธ๋ช ", requiredMode = Schema.RequiredMode.REQUIRED) | ||
@NotBlank | ||
String deviceModel | ||
) { | ||
public DeviceToken toEntity(Member member) { | ||
return DeviceToken.of(deviceToken, os, deviceModel, member); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...pp-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package kr.co.fitapet.api.apis.test; | ||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import kr.co.fitapet.api.common.security.authentication.CustomUserDetails; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "ํธ์ ํ ์คํธ API", description = "push notification์ ์ํ ๊ฐ๋ฐ์์ฉ ์์ API ์ ๋๋ค. ํ ์คํธ ํ ์ญ์ ์์ ") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@RequestMapping("/api/v2/test/members") | ||
public class NotificationTestController { | ||
private final NotificationTestService notificationTestService; | ||
|
||
@RequestMapping("/push") | ||
@PreAuthorize("isAuthenticated()") | ||
public void push(@AuthenticationPrincipal CustomUserDetails user) { | ||
notificationTestService.push(user.getUserId()); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...t-app-external-api/src/main/java/kr/co/fitapet/api/apis/test/NotificationTestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package kr.co.fitapet.api.apis.test; | ||
|
||
import kr.co.fitapet.domain.domains.device.domain.DeviceToken; | ||
import kr.co.fitapet.domain.domains.device.service.DeviceTokenSearchService; | ||
import kr.co.fitapet.infra.client.fcm.NotificationDataKey; | ||
import kr.co.fitapet.infra.client.fcm.NotificationService; | ||
import kr.co.fitapet.infra.client.fcm.request.NotificationRequest; | ||
import kr.co.fitapet.infra.client.fcm.request.NotificationSingleRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class NotificationTestService { | ||
private final DeviceTokenSearchService deviceTokenSearchService; | ||
private final NotificationService notificationService; | ||
|
||
public void push(Long userId) { | ||
log.info("push to {}", userId); | ||
|
||
List<DeviceToken> deviceTokens = deviceTokenSearchService.findDeviceTokensByMemberId(userId); | ||
log.info("deviceTokens: {}", deviceTokens); | ||
|
||
log.info("send push message"); | ||
deviceTokens.forEach(deviceToken -> { | ||
NotificationSingleRequest request = NotificationSingleRequest.builder() | ||
.token(deviceToken.getDeviceToken()) | ||
.title("ํธ์ ํ ์คํธ") | ||
.content("ํธ์ ํ ์คํธ ๋ฉ์์ง") | ||
.build(); | ||
|
||
notificationService.sendMessage(request); | ||
}); | ||
|
||
log.info("send push message with image"); | ||
deviceTokens.forEach(deviceToken -> { | ||
NotificationSingleRequest request = NotificationSingleRequest.builder() | ||
.title("์ด๋ฏธ์ง ํ ์คํธ") | ||
.content("ํธ์ ์ด๋ฏธ์ง ํ ์คํธ ๋ฉ์์ง") | ||
.imageUrl("https://pkcy.kr.object.ncloudstorage.com/profile/0bb25dde9020.jpeg") | ||
.build(); | ||
|
||
notificationService.sendMessage(request); | ||
}); | ||
|
||
log.info("send push message with data"); | ||
deviceTokens.forEach(deviceToken -> { | ||
NotificationSingleRequest request = NotificationSingleRequest.builder() | ||
.title("๋ฐ์ดํฐ ํ ์คํธ") | ||
.content("ํธ์ ๋ฐ์ดํฐ ํ ์คํธ ๋ฉ์์ง") | ||
.data( | ||
Map.of( | ||
NotificationDataKey.NOTICE_TYPE.getField(), "TEST_TYPE", | ||
NotificationDataKey.TO_ID.getField(), userId.toString() | ||
) | ||
) | ||
.build(); | ||
|
||
notificationService.sendMessage(request); | ||
}); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...rnal-api/src/main/java/kr/co/fitapet/api/common/event/notification/AnnouncementEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package kr.co.fitapet.api.common.event.notification; | ||
|
||
import kr.co.fitapet.domain.domains.notification.domain.Notification; | ||
import kr.co.fitapet.domain.domains.notification.type.NotificationType; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record AnnouncementEvent( | ||
String title, | ||
String content, | ||
String imageUrl | ||
) { | ||
public Notification toEntity(Long toId) { | ||
return Notification.builder() | ||
.title(title) | ||
.content(content) | ||
.imageUrl(imageUrl) | ||
.ctype(NotificationType.ANNOUNCEMENT) | ||
.toId(toId) | ||
.build(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...p-external-api/src/main/java/kr/co/fitapet/api/common/event/notification/NoticeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package kr.co.fitapet.api.common.event.notification; | ||
|
||
import kr.co.fitapet.domain.domains.notification.domain.Notification; | ||
import kr.co.fitapet.domain.domains.notification.type.NotificationType; | ||
import kr.co.fitapet.infra.client.fcm.NotificationDataKey; | ||
import lombok.Builder; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* FCM Push Notification Event๋ฅผ ์ํ Data Type Class | ||
* | ||
* @param memberId Long : ํธ์ ์๋ฆผ์ ๋ฐ์ ํ์ ID | ||
* @param noticeType {@link NoticeType} : push ์๋ฆผ์ ์ํ ์๋ฆผ ํ์ | ||
* @param notificationType {@link NotificationType} : DB์ ์ ์ฅ๋ ์๋ฆผ ํ์ | ||
* @param title String : ํธ์ ์๋ฆผ ์ ๋ชฉ | ||
* @param content String : ํธ์ ์๋ฆผ ๋ด์ฉ. {@link NoticeType}์ ๋ฐ๋ผ ๋์ ์ผ๋ก ์์ฑ | ||
* @param imageUrl String : ํธ์ ์๋ฆผ ์ด๋ฏธ์ง URL (optional) | ||
* @param data Map<{@link NotificationDataKey}, String> : ํธ์ ์๋ฆผ์ ํฌํจ๋ ๋ฐ์ดํฐ | ||
*/ | ||
@Builder | ||
public record NoticeEvent( | ||
Long memberId, | ||
NoticeType noticeType, | ||
NotificationType notificationType, | ||
String title, | ||
String content, | ||
String imageUrl, | ||
Map<NotificationDataKey, String> data | ||
) { | ||
public Notification toEntity() { | ||
Map<String, Long> idFields = getIdFields(); | ||
|
||
return Notification.builder() | ||
.title(title) | ||
.content(getFormattedContent()) | ||
.imageUrl(imageUrl) | ||
.ctype(notificationType) | ||
.fromId(idFields.getOrDefault(NotificationDataKey.FROM_ID.getField(), null)) | ||
.toId(idFields.getOrDefault(NotificationDataKey.TO_ID.getField(), null)) | ||
.domainId(idFields.getOrDefault(NotificationDataKey.DOMAIN_ID.getField(), null)) | ||
.subjectId(idFields.getOrDefault(NotificationDataKey.SUBJECT_ID.getField(), null)) | ||
.build(); | ||
} | ||
|
||
/** | ||
* data์ ์ ๋ณด๋ฅผ {@link NoticeType}์ ๋ฐ๋ผ ๋์ ์ผ๋ก ์์ฑ๋ content๋ฅผ ๋ฐํํฉ๋๋ค. | ||
* @return String : ๋์ ์ผ๋ก ์์ฑ๋ content | ||
*/ | ||
public String getFormattedContent() { | ||
return noticeType.createFormattedContent(getNameFields()); | ||
} | ||
|
||
private Map<String, Long> getIdFields() { | ||
return data.entrySet().stream() | ||
.filter(e -> e.getKey().isIdField()) | ||
.collect( | ||
Collectors.toMap( | ||
e -> e.getKey().getField(), | ||
e -> Long.parseLong(e.getValue()) | ||
) | ||
); | ||
} | ||
|
||
private Map<String, String> getNameFields() { | ||
return data.entrySet().stream() | ||
.filter(e -> e.getKey().isNameField()) | ||
.collect( | ||
Collectors.toMap( | ||
e -> e.getKey().getField(), | ||
Map.Entry::getValue | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.