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

fix: fcm 요청 시 잘못된 토큰은 null로 초기화 #314

Merged
merged 1 commit into from
Dec 30, 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 @@ -23,4 +23,6 @@ public interface MemberRepository extends JpaRepository<MemberEntity, Long> {
Page<MemberEntity> findAllByIsAgreeMarketing(boolean isAgreeMarketing, Pageable pageable);

void deleteById(Long memberId);

Optional<MemberEntity> findByFcmRegistrationToken(String fcmRegistrationToken);
}
3 changes: 3 additions & 0 deletions offroad-external/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
dependencies {

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

//s3
implementation("software.amazon.awssdk:bom:2.21.0")
implementation("software.amazon.awssdk:s3:2.21.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import site.offload.db.member.entity.MemberEntity;
import site.offload.enums.fcm.MessagePriority;
import site.offload.external.enums.ErrorMessage;
import site.offload.external.enums.FcmAlarmType;
import site.offload.external.exception.OffroadException;
import site.offload.external.fcm.dto.NotificationFieldsDto;
import site.offload.external.fcm.dto.SendMulticastRequest;
import site.offload.external.fcm.dto.SendRequest;
import site.offload.external.member.MemberExternalUseCase;

import java.util.HashMap;
import java.util.List;
Expand All @@ -23,6 +23,7 @@
public class MessagingService {

private final FirebaseMessaging firebaseMessaging;
private final MemberExternalUseCase memberExternalUseCase;

private static final String ANDROID_CLICK_ACTION_DATA_KEY = "type";
private static final String ANDROID_NOTIFICATION_TITLE_KEY = "title";
Expand All @@ -46,7 +47,10 @@ public void send(final SendRequest request) {
final String errorCode = String.valueOf(exception.getMessagingErrorCode());
log.error("Failed to send fcm message - token: {}, reason: {}", token, errorCode);

throw new OffroadException(ErrorMessage.FAIL_TO_SEND_MESSAGE);
final MemberEntity member = memberExternalUseCase.getMemberByFcmToken(request.registrationToken());
if (member != null) {
memberExternalUseCase.updateFcmToken(null, member);
}
}
}

Expand All @@ -64,7 +68,6 @@ public void sendMulticast(final SendMulticastRequest request) {
exception.getMessage(),
exception.getMessagingErrorCode());

throw new OffroadException(ErrorMessage.FAIL_TO_SEND_MESSAGE);
}
}

Expand Down Expand Up @@ -114,6 +117,10 @@ private void handleMulticastSendingFailure(final BatchResponse response, final S
final String errorCode = sendResponse.getException().getMessagingErrorCode().name();

log.error("Failed to send fcm multicast messages - token: {}, reason: {}", failedToken, errorCode);
final MemberEntity member = memberExternalUseCase.getMemberByFcmToken(failedToken);
if (member != null) {
memberExternalUseCase.updateFcmToken(null, member);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package site.offload.external.member;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.offload.db.member.entity.MemberEntity;
import site.offload.db.member.repository.MemberRepository;

@Service
@RequiredArgsConstructor
public class MemberExternalService {

private final MemberRepository memberRepository;

public MemberEntity getMemberByFcmToken(String fcmToken) {
return memberRepository.findByFcmRegistrationToken(fcmToken).orElse(null);
}

@Transactional
public void updateFcmToken(final String token, final MemberEntity member) {
member.updateFcmRegistrationToken(token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package site.offload.external.member;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import site.offload.db.member.entity.MemberEntity;

@Component
@RequiredArgsConstructor
public class MemberExternalUseCase {

private final MemberExternalService memberExternalService;

public MemberEntity getMemberByFcmToken(String fcmToken) {
return memberExternalService.getMemberByFcmToken(fcmToken);
}

public void updateFcmToken(final String token, final MemberEntity member) {
memberExternalService.updateFcmToken(token, member);
}
}