Skip to content

Commit

Permalink
Merge pull request #132 from Modagbul/feat/alarm_minsu
Browse files Browse the repository at this point in the history
로그아웃, 알림 Off한 경우에 push 알림은 안보내고 알림 히스토리에는 저장되게 수정
  • Loading branch information
minsu20 authored Dec 10, 2023
2 parents dd83b23 + 6d68870 commit ad6347b
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.moing.backend.domain.board.domain.entity.Board;
import com.moing.backend.domain.history.application.dto.response.MemberIdAndToken;
import com.moing.backend.domain.history.application.mapper.AlarmHistoryMapper;
import com.moing.backend.domain.history.application.service.SaveMultiAlarmHistoryUseCase;
import com.moing.backend.domain.history.domain.entity.AlarmType;
import com.moing.backend.domain.history.domain.entity.PagePath;
import com.moing.backend.domain.member.domain.entity.Member;
Expand Down Expand Up @@ -37,11 +35,10 @@ public void sendNewUploadAlarm(BaseServiceResponse baseServiceResponse, Board bo
if (board.isNotice()) {
String title = NEW_NOTICE_UPLOAD_MESSAGE.title(team.getName());
String body = NEW_NOTICE_UPLOAD_MESSAGE.body(board.getTitle());
Optional<List<MemberIdAndToken>> memberIdAndTokens = teamMemberGetService.getMemberInfoExceptMe(team.getTeamId(), member.getMemberId());
if (memberIdAndTokens.isPresent() && !memberIdAndTokens.get().isEmpty()) {
//알림 보내기
eventPublisher.publishEvent(new MultiFcmEvent(title, body, memberIdAndTokens.get(), createIdInfo(team.getTeamId(), board.getBoardId()), team.getName(), AlarmType.NEW_UPLOAD, PagePath.NOTICE_PATH.getValue()));
}
Optional<List<MemberIdAndToken>> memberIdAndTokensByPush = teamMemberGetService.getNewUploadPushInfo(team.getTeamId(), member.getMemberId());
Optional<List<MemberIdAndToken>> memberIdAndTokensBySave = teamMemberGetService.getNewUploadSaveInfo(team.getTeamId(), member.getMemberId());
// 알림 보내기
eventPublisher.publishEvent(new MultiFcmEvent(title, body, memberIdAndTokensByPush, memberIdAndTokensBySave, createIdInfo(team.getTeamId(), board.getBoardId()), team.getName(), AlarmType.NEW_UPLOAD, PagePath.NOTICE_PATH.getValue()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void sendFireThrowAlarm(Member throwMember, Member receiveMember, Team te
String message = getMessage(throwMember.getNickName(), receiveMember.getNickName(), randomNum);
String idInfo = createIdInfo(mission.getType() == MissionType.REPEAT, mission.getTeam().getTeamId(), mission.getId());

eventPublisher.publishEvent(new SingleFcmEvent(receiveMember.getFcmToken(), title, message, receiveMember.getMemberId(), idInfo, team.getName(), AlarmType.FIRE, MISSION_PATH.getValue()));
eventPublisher.publishEvent(new SingleFcmEvent(receiveMember, title, message, idInfo, team.getName(), AlarmType.FIRE, MISSION_PATH.getValue()));
}

public String getMessage(String pusher, String receiver, int num) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class Member extends BaseTimeEntity {

private LocalDateTime lastSignInTime;

private boolean isSignOut;

@OneToMany(mappedBy = "member")
private List<TeamMember> teamMembers = new ArrayList<>(); //최대 3개이므로 양방향

Expand Down Expand Up @@ -165,4 +167,12 @@ public void deleteMember(){
this.isDeleted=true;
}

public void signOut() {
this.isSignOut = true;
}

public void signIn() {
this.isSignOut = false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Member saveMember(Member member) {
} else {
findMember.get().updateFcmToken(member.getFcmToken());
findMember.get().updateLastSignInTime(LocalDateTime.now());
findMember.get().signIn();
return findMember.get();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public void sendNewMissionUploadAlarm(Member member, Mission mission) {
String title = team.getName() + " " + NEW_SINGLE_MISSION_COMING.getTitle();
String message = mission.getTitle();

Optional<List<MemberIdAndToken>> memberIdAndTokens = teamMemberGetService.getMemberInfoExceptMe(team.getTeamId(), member.getMemberId());
if (memberIdAndTokens.isPresent() && !memberIdAndTokens.get().isEmpty()) {
eventPublisher.publishEvent(new MultiFcmEvent(title, message, memberIdAndTokens.get(), createIdInfo(team.getTeamId(), mission.getId()), team.getName(), AlarmType.NEW_UPLOAD, PagePath.MISSION_PATH.getValue()));
}
Optional<List<MemberIdAndToken>> memberIdAndTokensByPush = teamMemberGetService.getNewUploadPushInfo(team.getTeamId(), member.getMemberId());
Optional<List<MemberIdAndToken>> memberIdAndTokensBySave = teamMemberGetService.getNewUploadSaveInfo(team.getTeamId(), member.getMemberId());
// 알림 보내기
eventPublisher.publishEvent(new MultiFcmEvent(title, message, memberIdAndTokensByPush, memberIdAndTokensBySave, createIdInfo(team.getTeamId(), mission.getId()), team.getName(), AlarmType.NEW_UPLOAD, PagePath.MISSION_PATH.getValue()));
}

private String createIdInfo(Long teamId, Long missionId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.moing.backend.domain.mypage.application.service;

import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.member.domain.service.MemberGetService;
import com.moing.backend.global.config.security.jwt.TokenUtil;
import lombok.RequiredArgsConstructor;
Expand All @@ -11,8 +12,9 @@ public class SignOutUseCase {

private final TokenUtil tokenUtil;
private final MemberGetService memberGetService;

public void signOut(String socialId){
tokenUtil.expireRefreshToken(socialId);
Member member=memberGetService.getMemberBySocialId(socialId);
member.signOut();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void approveTeams(List<Long> teamIds){
String title=APPROVE_TEAM_MESSAGE.title(info.getLeaderName(), info.getTeamName());
String body= APPROVE_TEAM_MESSAGE.body();

eventPublisher.publishEvent(new SingleFcmEvent(info.getLeaderFcmToken(), title, body, info.getLeaderId(), "", info.getTeamName(), AlarmType.APPROVE_TEAM, HOME_PATH.getValue()));
// eventPublisher.publishEvent(new SingleFcmEvent(info.getLeaderFcmToken(), title, body, info.getLeaderId(), "", info.getTeamName(), AlarmType.APPROVE_TEAM, HOME_PATH.getValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void rejectTeams(List<Long> teamIds) {
String title = REJECT_TEAM_MESSAGE.title(info.getLeaderName(), info.getTeamName());
String body = REJECT_TEAM_MESSAGE.body();

eventPublisher.publishEvent(new SingleFcmEvent(info.getLeaderFcmToken(), title, body, info.getLeaderId(), "", info.getTeamName(), AlarmType.REJECT_TEAM, HOME_PATH.getValue()));
// eventPublisher.publishEvent(new SingleFcmEvent(info.getLeaderFcmToken(), title, body, info.getLeaderId(), "", info.getTeamName(), AlarmType.REJECT_TEAM, HOME_PATH.getValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

public interface TeamMemberCustomRepository {
List<Long> findMemberIdsByTeamId(Long teamId);
Optional<List<MemberIdAndToken>> findIdAndTokensByTeamIdAndMemberId(Long teamId, Long memberId);
Optional<List<MemberIdAndToken>> findNewUploadPushInfo(Long teamId, Long memberId);
Optional<List<MemberIdAndToken>> findNewUploadSaveInfo(Long teamId, Long memberId);
Optional<List<MemberIdAndToken>> findRemindPushInfo(Long teamId, Long memberId);
Optional<List<MemberIdAndToken>> findRemindSaveInfo(Long teamId, Long memberId);
Optional<List<String>> findFcmTokensByTeamIdAndMemberId(Long teamId, Long memberId);
List<TeamMemberInfo> findTeamMemberInfoByTeamId(Long teamId);
List<TeamMember> findTeamMemberByMemberId(Long memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,57 @@ public List<Long> findMemberIdsByTeamId(Long teamId) {
}

@Override
public Optional<List<MemberIdAndToken>> findIdAndTokensByTeamIdAndMemberId(Long teamId, Long memberId) {
public Optional<List<MemberIdAndToken>> findNewUploadPushInfo(Long teamId, Long memberId) {
List<MemberIdAndToken> result = queryFactory.select(Projections.constructor(MemberIdAndToken.class,
teamMember.member.fcmToken,
teamMember.member.memberId))
.from(teamMember)
.where(teamMember.team.teamId.eq(teamId)
.and(teamMember.member.isNewUploadPush.eq(true))
.and(teamMember.member.isSignOut.eq(false))
.and(teamMember.member.memberId.ne(memberId))
.and(teamMember.isDeleted.eq(false)))
.fetch();

return result.isEmpty() ? Optional.empty() : Optional.of(result);
}

@Override
public Optional<List<MemberIdAndToken>> findNewUploadSaveInfo(Long teamId, Long memberId) {
List<MemberIdAndToken> result = queryFactory.select(Projections.constructor(MemberIdAndToken.class,
teamMember.member.fcmToken,
teamMember.member.memberId))
.from(teamMember)
.where(teamMember.team.teamId.eq(teamId)
.and(teamMember.member.memberId.ne(memberId))
.and(teamMember.isDeleted.eq(false)))
.fetch();

return result.isEmpty() ? Optional.empty() : Optional.of(result);
}
@Override
public Optional<List<MemberIdAndToken>> findRemindPushInfo(Long teamId, Long memberId) {
List<MemberIdAndToken> result = queryFactory.select(Projections.constructor(MemberIdAndToken.class,
teamMember.member.fcmToken,
teamMember.member.memberId))
.from(teamMember)
.where(teamMember.team.teamId.eq(teamId)
.and(teamMember.member.isRemindPush.eq(true))
.and(teamMember.member.isSignOut.eq(false))
.and(teamMember.member.memberId.ne(memberId))
.and(teamMember.isDeleted.eq(false)))
.fetch();

return result.isEmpty() ? Optional.empty() : Optional.of(result);
}

@Override
public Optional<List<MemberIdAndToken>> findRemindSaveInfo(Long teamId, Long memberId) {
List<MemberIdAndToken> result = queryFactory.select(Projections.constructor(MemberIdAndToken.class,
teamMember.member.fcmToken,
teamMember.member.memberId))
.from(teamMember)
.where(teamMember.team.teamId.eq(teamId)
.and(teamMember.member.memberId.ne(memberId))
.and(teamMember.isDeleted.eq(false)))
.fetch();
Expand All @@ -52,6 +96,7 @@ public Optional<List<String>> findFcmTokensByTeamIdAndMemberId(Long teamId, Long
.from(teamMember)
.where(teamMember.team.teamId.eq(teamId)
.and(teamMember.member.isNewUploadPush.eq(true))
.and(teamMember.member.isSignOut.eq(false))
.and(teamMember.member.memberId.ne(memberId))
.and(teamMember.isDeleted.eq(false)))
.fetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,20 @@ public List<TeamMember> getNotDeletedTeamMember(Long memberId){
return teamMemberRepository.findTeamMemberByMemberId(memberId);
}

public Optional<List<MemberIdAndToken>> getMemberInfoExceptMe(Long teamId, Long memberId) {
return teamMemberRepository.findIdAndTokensByTeamIdAndMemberId(teamId, memberId);
public Optional<List<MemberIdAndToken>> getNewUploadPushInfo(Long teamId, Long memberId) {
return teamMemberRepository.findNewUploadPushInfo(teamId, memberId);
}

public Optional<List<MemberIdAndToken>> getNewUploadSaveInfo(Long teamId, Long memberId) {
return teamMemberRepository.findNewUploadSaveInfo(teamId, memberId);
}

public Optional<List<MemberIdAndToken>> getRemindPushInfo(Long teamId, Long memberId) {
return teamMemberRepository.findRemindPushInfo(teamId, memberId);
}

public Optional<List<MemberIdAndToken>> getRemindSaveInfo(Long teamId, Long memberId) {
return teamMemberRepository.findRemindSaveInfo(teamId, memberId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import lombok.Getter;

import java.util.List;
import java.util.Optional;

@Getter
@AllArgsConstructor
public class MultiFcmEvent {

private String title;
private String body;
private List<MemberIdAndToken> memberIdAndTokens;
private Optional<List<MemberIdAndToken>> idAndTokensByPush;
private Optional<List<MemberIdAndToken>> idAndTokensBySave;
private String idInfo;
private String name;
private AlarmType alarmType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.moing.backend.domain.history.application.dto.response.MemberIdAndToken;
import com.moing.backend.domain.history.domain.entity.AlarmType;
import com.moing.backend.domain.member.domain.entity.Member;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -11,10 +12,9 @@
@AllArgsConstructor
public class SingleFcmEvent {

private String registrationToken;
private Member member;
private String title;
private String body;
private Long memberId;
private String idInfo;
private String name;
private AlarmType alarmType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
public class FcmService {

private final FirebaseMessaging firebaseMessaging;
private final SaveMultiAlarmHistoryUseCase saveMultiAlarmHistoryUseCase;
private final SaveSingleAlarmHistoryUseCase saveSingleAlarmHistoryUseCase;

@Retryable(value = FirebaseMessagingException.class, maxAttempts = 3, backoff = @Backoff(delay = 1000))
public SingleResponse sendSingleDevice(SingleRequest toSingleRequest) {

Expand Down Expand Up @@ -67,7 +64,7 @@ public SingleResponse sendSingleDevice(SingleRequest toSingleRequest) {

try {
String response = firebaseMessaging.send(message);
saveSingleAlarmHistoryUseCase.saveAlarmHistory(toSingleRequest.getMemberId(), toSingleRequest.getIdInfo(), toSingleRequest.getTitle(), toSingleRequest.getBody(), toSingleRequest.getName(), toSingleRequest.getAlarmType(), toSingleRequest.getPath());
// saveSingleAlarmHistoryUseCase.saveAlarmHistory(toSingleRequest.getMemberId(), toSingleRequest.getIdInfo(), toSingleRequest.getTitle(), toSingleRequest.getBody(), toSingleRequest.getName(), toSingleRequest.getAlarmType(), toSingleRequest.getPath());
return new SingleResponse(response);
} catch (FirebaseMessagingException e) {
throw handleException(e);
Expand Down Expand Up @@ -123,7 +120,6 @@ public MultiResponse sendMultipleDevices(MultiRequest toMultiRequest) {
List<SendResponse> responses = response.getResponses();

List<Long> memberIds = AlarmHistoryMapper.getMemberIds(toMultiRequest.getMemberIdAndTokens());
saveMultiAlarmHistoryUseCase.saveAlarmHistories(memberIds, toMultiRequest.getIdInfo(), toMultiRequest.getTitle(), toMultiRequest.getBody(), toMultiRequest.getName(), toMultiRequest.getAlarmType(), toMultiRequest.getPath());

for (int i = 0; i < responses.size(); i++) {
if (!responses.get(i).isSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.moing.backend.global.config.fcm.util;

import com.moing.backend.domain.history.application.mapper.AlarmHistoryMapper;
import com.moing.backend.domain.history.application.service.SaveMultiAlarmHistoryUseCase;
import com.moing.backend.domain.history.application.service.SaveSingleAlarmHistoryUseCase;
import com.moing.backend.global.config.fcm.dto.event.MultiFcmEvent;
import com.moing.backend.global.config.fcm.dto.event.SingleFcmEvent;
import com.moing.backend.global.config.fcm.dto.request.MultiRequest;
Expand All @@ -16,17 +19,27 @@
public class FcmMessageUtil {

private final FcmService fcmService;
private final SaveSingleAlarmHistoryUseCase saveSingleAlarmHistoryUseCase;
private final SaveMultiAlarmHistoryUseCase saveMultiAlarmHistoryUseCase;

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onMultiFcmEvent(MultiFcmEvent event) {
fcmService.sendMultipleDevices(new MultiRequest(event.getMemberIdAndTokens(), event.getTitle(), event.getBody(), event.getIdInfo(), event.getName(), event.getAlarmType(), event.getPath()));
if (event.getIdAndTokensByPush().isPresent() && !event.getIdAndTokensByPush().get().isEmpty()) {
fcmService.sendMultipleDevices(new MultiRequest(event.getIdAndTokensByPush().get(), event.getTitle(), event.getBody(), event.getIdInfo(), event.getName(), event.getAlarmType(), event.getPath()));
}
if (event.getIdAndTokensBySave().isPresent() && !event.getIdAndTokensBySave().get().isEmpty()) {
saveMultiAlarmHistoryUseCase.saveAlarmHistories(AlarmHistoryMapper.getMemberIds(event.getIdAndTokensBySave().get()), event.getIdInfo(), event.getTitle(), event.getBody(), event.getName(), event.getAlarmType(), event.getPath());
}
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void onSingleFcmEvent(SingleFcmEvent event) {
fcmService.sendSingleDevice(new SingleRequest(event.getRegistrationToken(), event.getTitle(), event.getBody(), event.getMemberId(), event.getIdInfo(), event.getName(), event.getAlarmType(), event.getPath()));
if (event.getMember().isFirePush() && !event.getMember().isSignOut()) { //알림 on, 로그아웃 안함
fcmService.sendSingleDevice(new SingleRequest(event.getMember().getFcmToken(), event.getTitle(), event.getBody(), event.getMember().getMemberId(), event.getIdInfo(), event.getName(), event.getAlarmType(), event.getPath()));
}
saveSingleAlarmHistoryUseCase.saveAlarmHistory(event.getMember().getMemberId(), event.getIdInfo(), event.getTitle(), event.getBody(), event.getName(), event.getAlarmType(), event.getPath());
}
}

Loading

0 comments on commit ad6347b

Please sign in to comment.