Skip to content

Commit

Permalink
Fcm 메세지에 deepLink 페이로드 설정 (#257)
Browse files Browse the repository at this point in the history
* feature: Fcm deepLink 페이로드 설정

* fix: FcmNotificationDto에서 DeepLink제거
  • Loading branch information
dbscks97 authored Sep 9, 2024
1 parent ff262ff commit 80bb48e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,16 @@ private void sendBoostNotification(
.map(FcmToken::getToken)
.orElseThrow(() -> new CustomException(ErrorCode.FAILED_TO_FIND_FCM_TOKEN));

String deepLink =
FcmNotification.generateDeepLink(
FcmNotificationType.BOOSTER, missionRecord.getId());

FcmMessage fcmMessage =
FcmMessage.of(
notificationConstants.getTitle(),
notificationConstants.getMessage(),
token);
token,
deepLink);
sqsMessageService.sendMessage(fcmMessage);

saveNotification(
Expand Down Expand Up @@ -237,8 +242,10 @@ private List<FcmNotification> buildNotificationList(
public void sendAndNotifications(String title, String message, List<String> tokens) {
List<List<String>> batches = createBatches(tokens, 10);

String deepLink = FcmNotification.generateDeepLink(FcmNotificationType.MISSION, null);

for (List<String> batch : batches) {
sqsMessageService.sendBatchMessages(batch, title, message);
sqsMessageService.sendBatchMessages(batch, title, message, deepLink);
}

List<FcmNotification> notifications = buildNotificationList(title, message, tokens);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.depromeet.stonebed.domain.fcm.domain;

public record FcmMessage(String title, String body, String token) {
public record FcmMessage(String title, String body, String token, String deepLink) {

public static FcmMessage of(String title, String body, String token) {
return new FcmMessage(title, body, token);
public static FcmMessage of(String title, String body, String token, String deepLink) {
return new FcmMessage(title, body, token, deepLink);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public class FcmNotification extends BaseTimeEntity {
@Schema(description = "딥링크 URL", example = "myapp://notification/1")
private String deepLink;

private static final String DEEP_LINK_PREFIX = "myapp://";

private FcmNotification(
FcmNotificationType type,
String title,
String message,
Member member,
Long targetId,
Boolean isRead,
String deepLink) {
Boolean isRead) {
this.type = type;
this.title = title;
this.message = message;
this.member = member;
this.targetId = targetId;
this.isRead = isRead;
this.deepLink = deepLink;
}

public static FcmNotification create(
Expand All @@ -63,19 +63,18 @@ public static FcmNotification create(
Member member,
Long targetId,
Boolean isRead) {
String deepLink = generateDeepLink(type, targetId);
return new FcmNotification(type, title, message, member, targetId, isRead, deepLink);
return new FcmNotification(type, title, message, member, targetId, isRead);
}

public void markAsRead() {
this.isRead = true;
}

private static String generateDeepLink(FcmNotificationType type, Long targetId) {
public static String generateDeepLink(FcmNotificationType type, Long targetId) {
if (type == FcmNotificationType.MISSION) {
return "myapp://mission";
return DEEP_LINK_PREFIX + "mission";
} else if (type == FcmNotificationType.BOOSTER) {
return "myapp://boost?id=" + targetId;
return DEEP_LINK_PREFIX + "boost?id=" + targetId;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public record FcmNotificationDto(
String imageUrl,
@Schema(description = "읽음 여부", example = "false") Boolean isRead,
@Schema(description = "타겟 ID", example = "1") Long targetId,
@Schema(description = "알림 전송 시간", example = "2024-08-17 13:31:19") LocalDateTime createdAt,
@Schema(description = "딥링크 URL", example = "myapp://notification/1") String deepLink) {
@Schema(description = "알림 전송 시간", example = "2024-08-17 13:31:19")
LocalDateTime createdAt) {

public static FcmNotificationDto from(
FcmNotification notification, MissionRecord missionRecord) {
Expand All @@ -30,7 +30,6 @@ public static FcmNotificationDto from(
imageUrl,
notification.getIsRead(),
notification.getTargetId(),
notification.getCreatedAt(),
notification.getDeepLink());
notification.getCreatedAt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public void sendMessage(Object message) {
}
}

public void sendBatchMessages(List<String> tokens, String title, String message) {
public void sendBatchMessages(
List<String> tokens, String title, String message, String deepLink) {
List<SendMessageBatchRequestEntry> entries = new ArrayList<>();
for (String token : tokens) {
try {
FcmMessage fcmMessage = FcmMessage.of(title, message, token);
FcmMessage fcmMessage = FcmMessage.of(title, message, token, deepLink);
String messageBody = objectMapper.writeValueAsString(fcmMessage);
SendMessageBatchRequestEntry entry =
SendMessageBatchRequestEntry.builder()
Expand Down

0 comments on commit 80bb48e

Please sign in to comment.