Skip to content

Commit

Permalink
Merge pull request #306 from depromeet/fix/#305-push-deeplink
Browse files Browse the repository at this point in the history
refactor: 딥링크 수정
  • Loading branch information
char-yb authored Oct 15, 2024
2 parents 0bed347 + acd8755 commit e7fa5e7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private void sendCommentNotification(MissionRecord missionRecord, Comment commen
missionRecordOwner,
FcmNotificationConstants.COMMENT,
missionRecord,
comment,
commentWriter);
}

Expand All @@ -100,6 +101,7 @@ private void sendCommentNotification(MissionRecord missionRecord, Comment commen
parentCommentWriter,
FcmNotificationConstants.RE_COMMENT,
missionRecord,
comment,
commentWriter);
}

Expand All @@ -109,6 +111,7 @@ private void sendCommentNotification(MissionRecord missionRecord, Comment commen
missionRecordOwner,
FcmNotificationConstants.RECORD_RE_COMMENT,
missionRecord,
comment,
commentWriter);
}

Expand All @@ -121,6 +124,7 @@ private void sendCommentNotification(MissionRecord missionRecord, Comment commen
recipient,
FcmNotificationConstants.RE_COMMENT,
missionRecord,
comment,
commentWriter);
}
}
Expand All @@ -130,6 +134,7 @@ private void sendNotification(
Member recipient,
FcmNotificationConstants notificationType,
MissionRecord missionRecord,
Comment comment,
Member commentWriter) {
String title = notificationType.getTitle();
String message = commentWriter.getProfile().getNickname() + notificationType.getMessage();
Expand All @@ -143,6 +148,7 @@ private void sendNotification(
message,
tokens,
missionRecord.getId(),
comment.getId(),
FcmNotificationType.valueOf(notificationTypeName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ public void saveNotification(
String message,
Long targetId,
Long memberId,
Boolean isRead) {
Boolean isRead,
String deepLink) {
Member member =
memberRepository
.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));

FcmNotification notification =
FcmNotification.create(type, title, message, member, targetId, isRead);
FcmNotification.createNotification(
type, title, message, member, targetId, isRead, deepLink);
notificationRepository.save(notification);
}

Expand Down Expand Up @@ -199,11 +201,6 @@ private Optional<String> validateTokenForMember(Member member) {
return getTokenForMember(member).filter(token -> !token.isEmpty());
}

private String generateDeepLink(MissionRecord missionRecord, long boostCount) {
return FcmNotification.generateDeepLink(
FcmNotificationType.BOOSTER, missionRecord.getId(), boostCount);
}

private void createAndSendFcmMessage(
String title, String message, String token, String deepLink) {
FcmMessage fcmMessage = FcmMessage.of(title, message, token, deepLink);
Expand All @@ -217,7 +214,9 @@ private void sendBoostNotification(
String token = validateTokenForMember(missionRecord.getMember()).orElse(null);
if (token == null) return;

String deepLink = generateDeepLink(missionRecord, boostCount);
String deepLink =
FcmNotification.generateDeepLink(
FcmNotificationType.BOOSTER, missionRecord.getId(), boostCount);
createAndSendFcmMessage(
notificationConstants.getTitle(),
notificationConstants.getMessage(),
Expand All @@ -230,7 +229,8 @@ private void sendBoostNotification(
notificationConstants.getMessage(),
missionRecord.getId(),
missionRecord.getMember().getId(),
false);
false,
deepLink);
}

public void markNotificationAsRead(Long notificationId) {
Expand All @@ -249,7 +249,8 @@ private List<FcmNotification> buildNotificationList(
String message,
List<String> tokens,
Long targetId,
FcmNotificationType notificationType) {
FcmNotificationType notificationType,
String deepLink) {
List<FcmNotification> notifications = new ArrayList<>();

for (String token : tokens) {
Expand All @@ -261,8 +262,8 @@ private List<FcmNotification> buildNotificationList(
() -> new CustomException(ErrorCode.FAILED_TO_FIND_FCM_TOKEN));

FcmNotification newNotification =
FcmNotification.create(
notificationType, title, message, member, targetId, false);
FcmNotification.createNotification(
notificationType, title, message, member, targetId, false, deepLink);
notifications.add(newNotification);
}

Expand All @@ -273,18 +274,24 @@ public void sendAndNotifications(
String title,
String message,
List<String> tokens,
Long sourceId,
Long targetId,
FcmNotificationType notificationType) {
List<List<String>> batches = createBatches(tokens, BATCH_SIZE);

String deepLink = FcmNotification.generateDeepLink(notificationType, targetId, null);

if (notificationType == FcmNotificationType.COMMENT
|| notificationType == FcmNotificationType.RE_COMMENT) {
deepLink = FcmNotification.generateCommentDeepLink(sourceId, targetId);
}

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

List<FcmNotification> notifications =
buildNotificationList(title, message, tokens, targetId, notificationType);
buildNotificationList(title, message, tokens, targetId, notificationType, deepLink);
notificationRepository.saveAll(notifications);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,26 @@ private FcmNotification(
String message,
Member member,
Long targetId,
Boolean isRead) {
Boolean isRead,
String deepLink) {
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(
public static FcmNotification createNotification(
FcmNotificationType type,
String title,
String message,
Member member,
Long targetId,
Boolean isRead) {
return new FcmNotification(type, title, message, member, targetId, isRead);
Boolean isRead,
String deepLink) {
return new FcmNotification(type, title, message, member, targetId, isRead, deepLink);
}

public void markAsRead() {
Expand All @@ -76,9 +79,11 @@ public static String generateDeepLink(
return DEEP_LINK_PREFIX + "mission";
} else if (type == FcmNotificationType.BOOSTER) {
return DEEP_LINK_PREFIX + "boost?id=" + targetId + "&type=" + boostCount;
} else if (type == FcmNotificationType.COMMENT || type == FcmNotificationType.RE_COMMENT) {
return DEEP_LINK_PREFIX + "comment?id=" + targetId;
}
return null;
}

public static String generateCommentDeepLink(Long recordId, Long commentId) {
return DEEP_LINK_PREFIX + "comment?recordId=" + recordId + "&commentId=" + commentId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void sendDailyNotification() {
List<String> tokens = fcmNotificationService.getAllTokens();

fcmNotificationService.sendAndNotifications(
title, message, tokens, null, FcmNotificationType.MISSION);
title, message, tokens, null, null, FcmNotificationType.MISSION);

log.info("모든 사용자에게 정규 알림 전송 및 저장 완료");
}
Expand All @@ -57,7 +57,7 @@ public void sendReminderToIncompleteMissions() {

List<String> tokens = getIncompleteMissionTokens();
fcmNotificationService.sendAndNotifications(
title, message, tokens, null, FcmNotificationType.MISSION);
title, message, tokens, null, null, FcmNotificationType.MISSION);

log.info("미완료 미션 사용자에게 리마인더 전송 및 저장 완료. 총 토큰 수: {}", tokens.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public class FcmNotificationServiceTest extends FixtureMonkeySetUp {

// when
fcmNotificationService.saveNotification(
FcmNotificationType.MISSION, "title", "message", 1L, member.getId(), false);
FcmNotificationType.MISSION,
"title",
"message",
1L,
member.getId(),
false,
"myapp://mission");

// then
verify(notificationRepository, times(1)).save(any(FcmNotification.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class FcmSchedulerTest extends FixtureMonkeySetUp {
eq("새로운 미션을 지금 시작해보세요!"),
eq(tokens),
eq(null),
eq(null),
eq(FcmNotificationType.MISSION));
}

Expand Down Expand Up @@ -115,6 +116,7 @@ public class FcmSchedulerTest extends FixtureMonkeySetUp {
eq("미션 종료까지 5시간 남았어요!"),
eq(tokens),
eq(null),
eq(null),
eq(FcmNotificationType.MISSION));
}
}

0 comments on commit e7fa5e7

Please sign in to comment.