From d5ae42697565539f07ff17ae44f72c0caef2c7d9 Mon Sep 17 00:00:00 2001 From: SCY Date: Wed, 23 Oct 2024 17:31:40 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20FCM=20null=20=ED=86=A0=ED=81=B0=EC=97=90?= =?UTF-8?q?=20=EB=8C=80=ED=95=9C=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20(#633)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/FcmNotificationSender.java | 44 ++++++++++++------- .../service/FcmNotificationSubscriber.java | 32 +++++++++++--- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSender.java b/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSender.java index 6acf1105f..fa5da82f4 100644 --- a/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSender.java +++ b/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSender.java @@ -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) { @@ -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("알림 메시지 전송 실패: {}", "유효하지 않은 토큰"); @@ -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); } } diff --git a/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSubscriber.java b/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSubscriber.java index 1283a78f5..5e6ad7b5c 100644 --- a/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSubscriber.java +++ b/backend/src/main/java/com/zzang/chongdae/notification/service/FcmNotificationSubscriber.java @@ -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; @@ -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 { @@ -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); } } @@ -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); } }