Skip to content

Commit

Permalink
Merge pull request #245 from TeamDon-tBe/feat/#244
Browse files Browse the repository at this point in the history
[FEAT]fcm알림에서 아이콘 배지 값 추가
  • Loading branch information
Hong0329 authored Jun 30, 2024
2 parents 75fa381 + 8407de6 commit 259cc5b
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void postCommentVer2(Long memberId, Long contentId, MultipartFile comment

if (Boolean.TRUE.equals(contentWritingMember.getIsPushAlarmAllowed())) {
String FcmMessageTitle = usingMember.getNickname() + "님이 답글을 작성했습니다.";

contentWritingMember.increaseFcmBadge();
FcmMessageDto commentFcmMessage = FcmMessageDto.builder()
.validateOnly(false)
.message(FcmMessageDto.Message.builder()
Expand All @@ -122,6 +122,7 @@ public void postCommentVer2(Long memberId, Long contentId, MultipartFile comment
.description("답글 푸시 알림")
.relateContentId(String.valueOf(contentId))
.build())
.badge(String.valueOf(contentWritingMember.getFcmBadge()))
.build())
.build();

Expand Down Expand Up @@ -182,7 +183,7 @@ public void likeComment(Long memberId, Long commentId, CommentLikedRequestDto co

if (Boolean.TRUE.equals(targetMember.getIsPushAlarmAllowed())) {
String FcmMessageTitle = triggerMember.getNickname() + "님이 " + targetMember.getNickname() + "님의 답글을 좋아합니다.";

targetMember.increaseFcmBadge();
FcmMessageDto commentLikeFcmMessage = FcmMessageDto.builder()
.validateOnly(false)
.message(FcmMessageDto.Message.builder()
Expand All @@ -196,6 +197,7 @@ public void likeComment(Long memberId, Long commentId, CommentLikedRequestDto co
.description("답글 좋아요 푸시 알림")
.relateContentId(String.valueOf(contentId))
.build())
.badge(String.valueOf(targetMember.getFcmBadge()))
.build())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.dontbe.www.DontBeServer.api.content.dto.response.*;
import com.dontbe.www.DontBeServer.api.content.service.ContentCommandService;
import com.dontbe.www.DontBeServer.api.content.service.ContentQueryService;
import com.dontbe.www.DontBeServer.api.member.service.MemberCommandService;
import com.dontbe.www.DontBeServer.common.response.ApiResponse;
import com.dontbe.www.DontBeServer.common.util.MemberUtil;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public void likeContent(Long memberId, Long contentId, ContentLikedRequestDto co
isDuplicateContentLike(content, triggerMember);

Member targetMember = memberRepository.findMemberByIdOrThrow(content.getMember().getId());
ContentLiked contentLiked = ContentLiked.builder()
ContentLiked contentLiked = ContentLiked.builder()
.content(content)
.member(triggerMember)
.build();
ContentLiked savedContentLiked = contentLikedRepository.save(contentLiked);


//위에가 게시물 좋아요 관련, 아래는 노티 테이블 채우기. 노티에 게시글 내용이 없어서 빈스트링 제공.
if(triggerMember != targetMember) { //자신 게시물에 대한 좋아요 누르면 알림 발생 x
if (triggerMember != targetMember) { //자신 게시물에 대한 좋아요 누르면 알림 발생 x
Notification notification = Notification.builder()
.notificationTargetMember(targetMember)
.notificationTriggerMemberId(triggerMember.getId())
Expand All @@ -117,7 +117,7 @@ public void likeContent(Long memberId, Long contentId, ContentLikedRequestDto co

if (Boolean.TRUE.equals(targetMember.getIsPushAlarmAllowed())) {
String FcmMessageTitle = triggerMember.getNickname() + "님이 " + targetMember.getNickname() + "님의 글을 좋아합니다.";

targetMember.increaseFcmBadge();
FcmMessageDto contentLikeFcmMessage = FcmMessageDto.builder()
.validateOnly(false)
.message(FcmMessageDto.Message.builder()
Expand All @@ -131,6 +131,7 @@ public void likeContent(Long memberId, Long contentId, ContentLikedRequestDto co
.description("게시글 좋아요 푸시 알림")
.relateContentId(String.valueOf(contentId))
.build())
.badge(String.valueOf(targetMember.getFcmBadge()))
.build())
.build();

Expand All @@ -139,6 +140,7 @@ public void likeContent(Long memberId, Long contentId, ContentLikedRequestDto co
}
}


public void unlikeContent(Long memberId, Long contentId) {
Member triggerMember = memberRepository.findMemberByIdOrThrow(memberId);
Content content = contentRepository.findContentByIdOrThrow(contentId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dontbe.www.DontBeServer.api.member.controller;

import com.dontbe.www.DontBeServer.api.member.dto.request.MemberPatchFcmBadgeRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberProfilePatchRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberWithdrawalPatchRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.ProfilePatchRequestDto;
Expand Down Expand Up @@ -84,4 +85,12 @@ public ResponseEntity<ApiResponse<Object>> checkMemberNickname(Principal princip
memberQueryService.checkNicknameValidate(memberId, nickname);
return ApiResponse.success(NICKNAME_CHECK_SUCCESS);
}

@PatchMapping(value = "fcmbadge")
@Operation(summary = "유저 아이콘 배지 값 수정 API입니다.", description = "FcmBadgePatch")
public ResponseEntity<ApiResponse<Object>> updateMemberFcmBadge(Principal principal, @RequestBody MemberPatchFcmBadgeRequestDto memberPatchFcmBadgeRequestDto) {
Long memberId = MemberUtil.getMemberId(principal);
memberCommandService.updateMemberFcmBadge(memberId,memberPatchFcmBadgeRequestDto);
return ApiResponse.success(UPDATE_FCM_BADGE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public class Member extends BaseTimeEntity {
@Column(name = "deleted_Reason")
private String deletedReason;

@Column(name = "fcm_badge", columnDefinition = "INTEGER DEFAULT 0")
private int fcmBadge;

@OneToMany(mappedBy = "notificationTargetMember",cascade = ALL)
private List<Notification> targetNotification = new ArrayList<>();

Expand All @@ -95,6 +98,7 @@ private Member(String nickname, SocialPlatform socialPlatform, String socialId,
this.memberGhost = 0;
this.memberEmail = memberEmail;
this.socialNickname = socialNickname;
this.fcmBadge = 0;
}

public void decreaseGhost() {
Expand Down Expand Up @@ -133,4 +137,10 @@ public void softDelete() {
public void updateMemberIsPushAlarmAllowed(boolean newIsPushAlarmAllowed) { this.isPushAlarmAllowed = newIsPushAlarmAllowed; }

public void updateMemberFcmToken(String newFcmToken) { this.fcmToken = newFcmToken; }

public void increaseFcmBadge() {this.fcmBadge++; }

public void resetFcmBadge() { this.fcmBadge = 0;}

public void updateFcmBadge(int newFcmBadge) { this.fcmBadge = newFcmBadge;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dontbe.www.DontBeServer.api.member.dto.request;

public record MemberPatchFcmBadgeRequestDto(
int fcmBadge
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.dontbe.www.DontBeServer.api.content.repository.ContentLikedRepository;
import com.dontbe.www.DontBeServer.api.content.repository.ContentRepository;
import com.dontbe.www.DontBeServer.api.member.domain.Member;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberPatchFcmBadgeRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberProfilePatchRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberWithdrawalPatchRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.ProfilePatchRequestDto;
Expand Down Expand Up @@ -167,6 +168,16 @@ public void updateMemberProfile2(Long memberId, MultipartFile multipartFile, Pro
Member savedMember = memberRepository.save(existingMember);
}

public void resetMemberFcmBadge(Long memberId) {
Member member = memberRepository.findMemberByIdOrThrow(memberId);
member.resetFcmBadge();
}

public void updateMemberFcmBadge(Long memberId, MemberPatchFcmBadgeRequestDto memberPatchFcmBadgeRequestDto) {
Member member = memberRepository.findMemberByIdOrThrow(memberId);
member.updateFcmBadge(Math.max(memberPatchFcmBadgeRequestDto.fcmBadge(), 0));
}

private static String removeBaseUrl(String fullUrl, String baseUrl) {
if (fullUrl.startsWith(baseUrl)) {
return fullUrl.substring(baseUrl.length());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dontbe.www.DontBeServer.api.notification.controller;

import com.dontbe.www.DontBeServer.api.member.service.MemberCommandService;
import com.dontbe.www.DontBeServer.api.notification.dto.response.NotificaitonCountResponseDto;
import com.dontbe.www.DontBeServer.api.notification.service.NotificationCommandService;
import com.dontbe.www.DontBeServer.api.notification.service.NotificationQueryService;
Expand All @@ -24,6 +25,7 @@
public class NotificationController {
private final NotificationCommandService notificationCommandService;
private final NotificationQueryService notificationQueryService;
private final MemberCommandService memberCommandService;

@PatchMapping("notification-check")
@Operation(summary = "노티 체크 API 입니다.",description = "NotificationCheck")
Expand Down Expand Up @@ -51,13 +53,15 @@ public ResponseEntity<ApiResponse<Object>> getNotification(Principal principal)
@Operation(summary = "페이지네이션이 적용된 노티 전체 리스트 조회 API 입니다.",description = "NotificationGetPagination")
public ResponseEntity<ApiResponse<Object>> getNotificationAllPagination(Principal principal,@RequestParam(value = "cursor") Long cursor) {
Long memberId = MemberUtil.getMemberId(principal);
memberCommandService.resetMemberFcmBadge(memberId);
return ApiResponse.success(NOTIFICATION_ALL_SUCCESS, notificationQueryService.getNotificationAllPagination(memberId, cursor));
}

@GetMapping("notifications")
@Operation(summary = "노티 전체 리스트 조회 ver3 API 입니다.",description = "GetNotificationsVer3")
public ResponseEntity<ApiResponse<Object>> getNotifications(Principal principal,@RequestParam(value = "cursor") Long cursor) {
Long memberId = MemberUtil.getMemberId(principal);
memberCommandService.resetMemberFcmBadge(memberId);
return ApiResponse.success(NOTIFICATION_ALL_SUCCESS, notificationQueryService.getNotifications(memberId, cursor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void popularContentNotification() {

if(Boolean.TRUE.equals(topContentWriter.getIsPushAlarmAllowed())) {
String FcmMessageTitle = topContentWriter.getNickname() + "님이 작성하신 글이 인기들로 선정 되었어요.";

topContentWriter.increaseFcmBadge();
FcmMessageDto popularContentFcmMessage = FcmMessageDto.builder()
.validateOnly(false)
.message(FcmMessageDto.Message.builder()
Expand All @@ -83,6 +83,7 @@ public void popularContentNotification() {
.description("인기글 관련 푸시 알림")
.relateContentId(String.valueOf(topContent.getId()))
.build())
.badge(String.valueOf(topContentWriter.getFcmBadge()))
.build())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum SuccessStatus {
CONTENT_UNLIKE_SUCCESS(HttpStatus.OK,"게시글 좋아요 취소 성공"),
GET_CONTENT_ALL_SUCCESS(HttpStatus.OK, "게시물 리스트 조회 성공"),
GET_MEMBER_CONTENT_SUCCESS(HttpStatus.OK,"유저에 해당하는 게시글 리스트 조회"),
UPDATE_FCM_BADGE_SUCCESS(HttpStatus.OK, "유저의 배지값 수정 완료"),

/**
* comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class Message {
private NotificationDetails notificationDetails;
private String token;
private Data data;
private String badge;
}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public void sendMessage(FcmMessageDto fcmMessageDto) {
.build()
)
.putAllData(objectMapper.convertValue(fcmMessageDto.getMessage().getData(), Map.class))
.putData("badge", fcmMessageDto.getMessage().getBadge())
.build();

System.out.println(fcmMessageDto.getMessage().getBadge());
try {
FirebaseMessaging.getInstance().send(message);
} catch (FirebaseMessagingException e) {
Expand Down

0 comments on commit 259cc5b

Please sign in to comment.