Skip to content

Commit

Permalink
[OING-336] feat: 푸쉬 알림 메세지 변경 및 미션 업로드 시 노티 발송 (#258)
Browse files Browse the repository at this point in the history
* feat: update pick notification

* feat: send notification if mission unlocked

* feat: send survival noti only on survival post

* refactor: move mission message to another method
  • Loading branch information
CChuYong authored May 28, 2024
1 parent 4edb95b commit 52e6297
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.oing.domain.Comment;
import com.oing.domain.Member;
import com.oing.domain.Post;
import com.oing.domain.PostType;
import com.oing.service.FCMNotificationService;
import com.oing.service.MemberDeviceService;
import com.oing.service.MemberService;
import com.oing.service.PostService;
import com.oing.util.FCMNotificationUtil;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,6 +27,7 @@ public class FamilyNotificationEventListener {
public final MemberService memberService;
private final MemberDeviceService memberDeviceService;
private final FCMNotificationService fcmNotificationService;
private final PostService memberPostService;

@Transactional(Transactional.TxType.REQUIRES_NEW)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
Expand All @@ -40,22 +43,47 @@ public void onPostCreatedEvent(PostCreatedEvent postCreatedEvent) {
}

if(targetFcmTokens.isEmpty()) return;
MulticastMessage multicastMessage = MulticastMessage.builder()
.setNotification(
FCMNotificationUtil.buildNotification("삐삐",
String.format("%s님이 생존신고를 완료했어요.", author.getName()))
)
.putData("aosDeepLink", "post/view/" + post.getId())
.putData("iosDeepLink", "post/view/" + post.getId() + "?openComment=false&dateOfPost="
+ post.getCreatedAt().toLocalDate().toString())
.addAllTokens(targetFcmTokens)
.setApnsConfig(FCMNotificationUtil.buildApnsConfig())
.setAndroidConfig(FCMNotificationUtil.buildAndroidConfig())
.build();
fcmNotificationService.sendMulticastMessage(multicastMessage);
if(post.getType() == PostType.SURVIVAL) {
MulticastMessage multicastMessage = MulticastMessage.builder()
.setNotification(
FCMNotificationUtil.buildNotification("삐삐",
String.format("%s님이 생존신고를 완료했어요.", author.getName()))
)
.putData("aosDeepLink", "post/view/" + post.getId())
.putData("iosDeepLink", "post/view/" + post.getId() + "?openComment=false&dateOfPost="
+ post.getCreatedAt().toLocalDate().toString())
.addAllTokens(targetFcmTokens)
.setApnsConfig(FCMNotificationUtil.buildApnsConfig())
.setAndroidConfig(FCMNotificationUtil.buildAndroidConfig())
.build();
fcmNotificationService.sendMulticastMessage(multicastMessage);

if(memberPostService.isNewPostMadeMissionUnlocked(familyId)) {
sendMissionUnlockedMessages(familyMemberIds);
}
}
}
}

private void sendMissionUnlockedMessages(List<String> familyMemberIds) {
// 방금 막 미션이 언락 되었다면..
HashSet<String> missionTargetFcmTokens = new HashSet<>();
for (String familyMemberId : familyMemberIds) {
missionTargetFcmTokens.addAll(memberDeviceService.getFcmTokensByMemberId(familyMemberId));
}

MulticastMessage missionUnlockedMessage = MulticastMessage.builder()
.setNotification(
FCMNotificationUtil.buildNotification("열쇠를 획득해 미션 잠금이 해제되었어요!",
"사진 한 장을 더 찍을 수 있어요.")
)
.addAllTokens(missionTargetFcmTokens)
.setApnsConfig(FCMNotificationUtil.buildApnsConfig())
.setAndroidConfig(FCMNotificationUtil.buildAndroidConfig())
.build();
fcmNotificationService.sendMulticastMessage(missionUnlockedMessage);
}

@Transactional(Transactional.TxType.REQUIRES_NEW)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onPostCommentCreatedEvent(CommentCreatedEvent commentCreatedEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public PaginationResponse<FamilyMemberProfileResponse> getFamilyMembersProfiles(
@Override
public MemberResponse getMember(String memberId, String loginFamilyId) {
validateFamilyMember(memberId, loginFamilyId);

Member member = memberService.getMemberByMemberId(memberId);
return MemberResponse.of(member);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public DefaultResponse pickMember(String memberId, String loginMemberId, String
List<String> tokens = memberDeviceService.getFcmTokensByMemberId(toMember.getId());
if (!tokens.isEmpty()) {
Notification notification = FCMNotificationUtil
.buildNotification(String.format("%s님, 살아있나요?", toMember.getName()),
.buildNotification(String.format("%s님, 바쁘신가요?", toMember.getName()),
String.format("%s님이 당신의 생존을 궁금해해요.", fromMember.getName()));
MulticastMessage message = MulticastMessage.builder()
.setNotification(notification)
Expand Down
7 changes: 7 additions & 0 deletions post/src/main/java/com/oing/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ public boolean isCreatedSurvivalPostByMajority(String familyId) {
return survivalPostCount >= totalFamilyMembers / 2;
}

public boolean isNewPostMadeMissionUnlocked(String familyId) {
int totalFamilyMembers = postRepository.countFamilyMembersByFamilyIdAtYesterday(familyId);
int survivalPostCount = postRepository.countTodaySurvivalPostsByFamilyId(familyId);
return ((survivalPostCount - 1) < totalFamilyMembers / 2) // 방금 글 올리기 전에는 조건 만족 X but,
&& (survivalPostCount >= totalFamilyMembers / 2); //올리고 나서는 조건 만족
}

public int calculateRemainingSurvivalPostCountUntilMissionUnlocked(String familyId) {
int familyMemberCount = postRepository.countFamilyMembersByFamilyIdAtYesterday(familyId);
int requiredSurvivalPostCount = familyMemberCount / 2;
Expand Down

0 comments on commit 52e6297

Please sign in to comment.