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

refactor: 알림 시간 변경 및 예외처리 수정 #166

Merged
merged 1 commit into from
Oct 30, 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
23 changes: 15 additions & 8 deletions src/main/java/yerong/wedle/common/config/FMCConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,32 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
@Slf4j
public class FMCConfig {

@Value("${firebase.config}")
private String firebaseConfig;

@PostConstruct
private void init() throws IOException {
ByteArrayInputStream serviceAccountStream = new ByteArrayInputStream(firebaseConfig.getBytes());

FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccountStream))
.build();

FirebaseApp.initializeApp(options);
private void init() {
try (ByteArrayInputStream serviceAccount = new ByteArrayInputStream(firebaseConfig.getBytes())) {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();

FirebaseApp.initializeApp(options);
log.info("파이어베이스 서버와의 연결에 성공했습니다.");
} catch (IOException e) {
log.error("파이어베이스 서버와의 연결에 실패했습니다.", e);
}
}
}
60 changes: 42 additions & 18 deletions src/main/java/yerong/wedle/common/utils/FcmUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.Date;
import java.util.List;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,47 +26,70 @@ public class FcmUtils {
public static void broadCast(final List<String> registrationTokens, String title, String body) {
limitSizeValidate(registrationTokens);

MulticastMessage message = MulticastMessage.builder()
log.info("registrationTokens = {}", registrationTokens.get(0));
log.info("title = {}", title);
log.info("body = {}", body);

if (title == null || title.isEmpty()) {
log.error("제목이 비어 있습니다.");
return;
}
if (body == null || body.isEmpty()) {
log.error("본문이 비어 있습니다.");
return;
}

long currentTime = new Date().getTime() / 1000;
if (EXPIRED_TIME_FOR_UNIX <= currentTime) {
log.error("APNs 만료 시간 설정이 잘못되었습니다.");
}

MulticastMessage message = buildMessage(registrationTokens, title, body);

try {
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
pushSuccessValidate(registrationTokens, response);
} catch (FirebaseMessagingException e) {
log.error("FCM 메시지 전송 중 예외 발생: {}", e.getMessage(), e);
if (e.getErrorCode() != null) {
log.error("Error Code: {}", e.getErrorCode());
}
}
}

private static MulticastMessage buildMessage(List<String> registrationTokens, String title, String body) {
return MulticastMessage.builder()
.setNotification(Notification.builder()
.setTitle(title)
.setBody(body)
.build())
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps
.builder()
.setAps(Aps.builder()
.setAlert(body)
.build())
.putHeader("apns-expiration", Long.toString(EXPIRED_TIME_FOR_UNIX))
.build())
.addAllTokens(registrationTokens)
.build();

try {
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
pushSuccessValidate(registrationTokens, response);
} catch (FirebaseMessagingException e) {
log.error("FCM 메시지 전송 중 예외 발생: {}", e.getMessage(), e);
for (String token : registrationTokens) {
log.error("유효하지 않은 토큰: {}", token);
}
}
}


private static void limitSizeValidate(final List<String> registrationTokens) {
if (registrationTokens.size() > FCM_PUSH_LIMIT_SIZE) {
throw new IllegalArgumentException("FCM push 알림 수신자는 최대 500명입니다.");
}
}

private static void pushSuccessValidate(final List<String> registrationTokens, final BatchResponse response) {
System.out.println(response.getSuccessCount() + " messages were sent successfully.");
log.info("{} messages were sent successfully.", response.getSuccessCount());
if (response.getFailureCount() > 0) {
System.out.println(response.getFailureCount() + " messages failed to send.");
log.error("{} messages failed to send.", response.getFailureCount());
response.getResponses().forEach(sendResponse -> {
if (!sendResponse.isSuccessful()) {
System.out.println("Failed to send message to token: " + sendResponse.getMessageId());
System.out.println("Error: " + sendResponse.getException().getMessage());
String messageId = sendResponse.getMessageId();
String errorMessage = sendResponse.getException() != null ? sendResponse.getException().getMessage() : "Unknown error";

log.error("Failed to send message to token: {}. Error: {}",
messageId != null ? messageId : "null", errorMessage);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private CalendarEvent getCalendarEventById(Long calendarId) {
}

@Transactional
@Scheduled(cron = "0 18 19 * * ?", zone = "Asia/Seoul")
@Scheduled(cron = "0 42 22 * * ?", zone = "Asia/Seoul")
public void sendNotifications() {
LocalDate today = LocalDate.now();
List<Notification> dueNotifications = notificationRepository.findByNotificationDate(today);
Expand Down
Loading