Skip to content

Commit

Permalink
fix: FCM null 토큰에 대한 예외 처리 (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
helenason authored Oct 23, 2024
1 parent bc0c723 commit d5ae426
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
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);
}
}

0 comments on commit d5ae426

Please sign in to comment.