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 토큰에 대한 예외 처리 #633

Merged
merged 1 commit into from
Oct 23, 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 @@ -15,7 +15,9 @@
public class FcmNotificationSender implements NotificationSender {

private static final String ERROR_MESSAGE_WHEN_INVALID_TOKEN = "The registration token is not a valid FCM registration token";
private static final String ERROR_MESSAGE_WHEN_OLD_TOKEN = "Requested entity was not found.";
private static final String ERROR_MESSAGE_WHEN_OLD_TOKEN = "Requested entity was not found";
private static final String ERROR_MESSAGE_WHEN_TOKEN_EMPTY = "Exactly one of token, topic or condition must be specified";
private static final String ERROR_MESSAGE_WHEN_NULL_TOKEN = "registration tokens list must not contain null or empty strings";

@Override
public String send(Message message) {
Expand All @@ -28,6 +30,18 @@ public String send(Message message) {
}
}

@Override
public BatchResponse send(MulticastMessage message) {
try {
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
log.info("알림 메시지 전송 성공 개수: {}", response.getSuccessCount());
log.info("알림 메시지 전송 실패 개수: {}", response.getFailureCount());
return response;
} catch (FirebaseMessagingException e) {
return sendWhenFailWithBatch(e);
}
}

private String sendWhenFail(FirebaseMessagingException e) {
if (isInvalidToken(e)) {
log.error("알림 메시지 전송 실패: {}", "유효하지 않은 토큰");
Expand All @@ -38,22 +52,20 @@ private String sendWhenFail(FirebaseMessagingException e) {
throw new MarketException(NotificationErrorCode.CANNOT_SEND_ALARM);
}

private boolean isInvalidToken(FirebaseMessagingException e) {
return e.getMessage().contains(ERROR_MESSAGE_WHEN_INVALID_TOKEN)
|| e.getMessage().contains(ERROR_MESSAGE_WHEN_OLD_TOKEN);
private BatchResponse sendWhenFailWithBatch(FirebaseMessagingException e) {
if (isInvalidToken(e)) {
log.error("알림 메시지 전송 실패: {}", "유효하지 않은 토큰");
return null;
}
log.error("알림 메시지 전송 실패: {}", e.getMessage());
e.printStackTrace();
throw new MarketException(NotificationErrorCode.CANNOT_SEND_ALARM);
}

@Override
public BatchResponse send(MulticastMessage message) {
try {
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
log.info("알림 메시지 전송 성공 개수: {}", response.getSuccessCount());
log.info("알림 메시지 전송 실패 개수: {}", response.getFailureCount());
return response;
} catch (FirebaseMessagingException e) {
log.error("알림 메시지 전송 실패: {}", e.getMessage());
e.printStackTrace();
throw new MarketException(NotificationErrorCode.CANNOT_SEND_ALARM);
}
private boolean isInvalidToken(FirebaseMessagingException e) {
return e.getMessage().contains(ERROR_MESSAGE_WHEN_INVALID_TOKEN)
|| e.getMessage().contains(ERROR_MESSAGE_WHEN_OLD_TOKEN)
|| e.getMessage().contains(ERROR_MESSAGE_WHEN_TOKEN_EMPTY)
|| e.getMessage().contains(ERROR_MESSAGE_WHEN_NULL_TOKEN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.TopicManagementResponse;
import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.notification.domain.FcmTopic;
import com.zzang.chongdae.notification.exception.NotificationErrorCode;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -13,6 +15,11 @@
@Service
public class FcmNotificationSubscriber implements NotificationSubscriber {

private static final String ERROR_MESSAGE_WHEN_INVALID_TOKEN = "The registration token is not a valid FCM registration token";
private static final String ERROR_MESSAGE_WHEN_OLD_TOKEN = "Requested entity was not found";
private static final String ERROR_MESSAGE_WHEN_TOKEN_EMPTY = "Exactly one of token, topic or condition must be specified";
private static final String ERROR_MESSAGE_WHEN_NULL_TOKEN = "registration tokens list must not contain null or empty strings";

@Override
public TopicManagementResponse subscribe(MemberEntity member, FcmTopic topic) {
try {
Expand All @@ -22,9 +29,7 @@ public TopicManagementResponse subscribe(MemberEntity member, FcmTopic topic) {
log.info("구독 실패 개수: {}", response.getFailureCount());
return response;
} catch (FirebaseMessagingException e) {
log.error("토픽 구독 실패: {}", e.getMessage());
e.printStackTrace();
throw new RuntimeException(e);
return subscribeWhenFail(e);
}
}

Expand All @@ -37,9 +42,24 @@ public TopicManagementResponse unsubscribe(MemberEntity member, FcmTopic topic)
log.info("구독 취소 실패 개수: {}", response.getFailureCount());
return response;
} catch (FirebaseMessagingException e) {
log.error("토픽 구독 실패: {}", e.getMessage());
e.printStackTrace();
throw new RuntimeException(e);
return subscribeWhenFail(e);
}
}

private TopicManagementResponse subscribeWhenFail(FirebaseMessagingException e) {
if (isInvalidToken(e)) {
log.error("토픽 구독 실패: {}", "유효하지 않은 토큰");
return null;
}
log.error("토픽 구독 실패: {}", e.getMessage());
e.printStackTrace();
throw new MarketException(NotificationErrorCode.CANNOT_SEND_ALARM);
}

private boolean isInvalidToken(FirebaseMessagingException e) {
return e.getMessage().contains(ERROR_MESSAGE_WHEN_INVALID_TOKEN)
|| e.getMessage().contains(ERROR_MESSAGE_WHEN_OLD_TOKEN)
|| e.getMessage().contains(ERROR_MESSAGE_WHEN_TOKEN_EMPTY)
|| e.getMessage().contains(ERROR_MESSAGE_WHEN_NULL_TOKEN);
}
}