Skip to content

Commit

Permalink
fix: 유효하지 않은 FCM토큰 삭제 (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbscks97 authored Sep 24, 2024
1 parent eb9238e commit 94ca74a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.parsers.ReturnTypeParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -53,7 +54,7 @@ public class FcmNotificationService {
private static final long FIRST_BOOST_THRESHOLD = 1;
private static final long POPULAR_THRESHOLD = 1000;
private static final long SUPER_POPULAR_THRESHOLD = 5000;
private final ReturnTypeParser genericReturnTypeParser;
private static final int BATCH_SIZE = 10;

public void saveNotification(
FcmNotificationType type,
Expand Down Expand Up @@ -271,7 +272,7 @@ private List<FcmNotification> buildNotificationList(
}

public void sendAndNotifications(String title, String message, List<String> tokens) {
List<List<String>> batches = createBatches(tokens, 10);
List<List<String>> batches = createBatches(tokens, BATCH_SIZE);

String deepLink = FcmNotification.generateDeepLink(FcmNotificationType.MISSION, null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface FcmTokenRepository
List<FcmToken> findAllByMemberStatus(MemberStatus status);

List<FcmToken> findAllByUpdatedAtBefore(LocalDateTime cutoffDate);

void deleteByToken(String token);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.depromeet.stonebed.domain.sqs.application;

import com.depromeet.stonebed.domain.fcm.dao.FcmTokenRepository;
import com.depromeet.stonebed.domain.fcm.domain.FcmMessage;
import com.depromeet.stonebed.infra.properties.SqsProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -29,6 +29,7 @@ public class SqsMessageService {
private final SqsProperties sqsProperties;

private final ObjectMapper objectMapper;
private final FcmTokenRepository fcmTokenRepository;

public void sendMessage(Object message) {
try {
Expand All @@ -49,13 +50,14 @@ public void sendMessage(Object message) {
public void sendBatchMessages(
List<String> tokens, String title, String message, String deepLink) {
List<SendMessageBatchRequestEntry> entries = new ArrayList<>();
List<String> failedTokens = new ArrayList<>();
for (String token : tokens) {
try {
FcmMessage fcmMessage = FcmMessage.of(title, message, token, deepLink);
String messageBody = objectMapper.writeValueAsString(fcmMessage);
SendMessageBatchRequestEntry entry =
SendMessageBatchRequestEntry.builder()
.id(UUID.randomUUID().toString())
.id(token)
.messageBody(messageBody)
.build();
entries.add(entry);
Expand All @@ -64,25 +66,34 @@ public void sendBatchMessages(
}
}

SendMessageBatchRequest batchRequest =
SendMessageBatchRequest.builder()
.queueUrl(sqsProperties.queueUrl())
.entries(entries)
.build();
if (!entries.isEmpty()) {
SendMessageBatchRequest batchRequest =
SendMessageBatchRequest.builder()
.queueUrl(sqsProperties.queueUrl())
.entries(entries)
.build();

try {
SendMessageBatchResponse batchResponse = sqsClient.sendMessageBatch(batchRequest);
try {
SendMessageBatchResponse batchResponse = sqsClient.sendMessageBatch(batchRequest);

// 실패한 메시지 처리
List<BatchResultErrorEntry> failedMessages = batchResponse.failed();
if (!failedMessages.isEmpty()) {
// 실패한 메시지 처리
List<BatchResultErrorEntry> failedMessages = batchResponse.failed();
for (BatchResultErrorEntry failed : failedMessages) {
log.error("메시지 전송 실패, ID {}: {}", failed.id(), failed.message());
failedTokens.add(failed.id());
}
}

} catch (Exception e) {
log.error("SQS 배치 메시지 전송 실패: {}", e.getMessage());
// 실패한 토큰 삭제 등의 후속 작업
for (String failedToken : failedTokens) {
fcmTokenRepository.deleteByToken(failedToken);
log.info("비활성화된 FCM 토큰 삭제: {}", failedToken);
}

} catch (Exception e) {
log.error("SQS 배치 메시지 전송 실패: {}", e.getMessage());
}
} else {
log.warn("전송할 메시지가 없습니다.");
}
}
}

0 comments on commit 94ca74a

Please sign in to comment.