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

FCM 푸시 알림, 유효하지 않은 FCM토큰 삭제 #276

Merged
merged 1 commit into from
Sep 24, 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 @@ -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("전송할 메시지가 없습니다.");
}
}
}
Loading