Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OING-336] feat: 푸쉬 알림 메세지 변경 및 미션 업로드 시 노티 발송 #258

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onPostCreatedEvent에서 사용자가 글을 작성했을 때, 가족이 미션 키를 획득했을 때 두 가지 경우에 대해 모두 처리하고 있는데, 이 두 가지 경우를 각 메서드로 분리해서 책임을 명확히 하면 더 좋을 거 같아요!

}
}

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
Loading