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: 알림 처리 시간 변경 및 예외처리 추가 #162

Merged
merged 7 commits 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
32 changes: 23 additions & 9 deletions src/main/java/yerong/wedle/common/utils/FcmUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,51 @@
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.MulticastMessage;
import com.google.firebase.messaging.Notification;

import java.util.Date;
import java.util.List;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FcmUtils {

private static final int FCM_PUSH_LIMIT_SIZE = 500;
private static final long ONE_WEEK = (long) 60 * 60 * 24 * 7;
private static final long EXPIRED_TIME_FOR_UNIX = new Date(new Date().getTime() + ONE_WEEK).getTime();
private static final long ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7;
private static final long EXPIRED_TIME_FOR_UNIX = new Date().getTime() / 1000 + ONE_WEEK_IN_SECONDS;

public static void broadCast(final List<String> registrationTokens, String title, String body) {
limitSizeValidate(registrationTokens);

MulticastMessage message = MulticastMessage.builder()
.setNotification(Notification.builder()
.setTitle(title) // 알림 제목
.setBody(body) // 알림 내용
.setTitle(title)
.setBody(body)
.build())
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder().setAlert(body).build()) // iOS에 맞춰 알림 내용 설정
.setAps(Aps
.builder()
.setAlert(body)
.build())
.putHeader("apns-expiration", Long.toString(EXPIRED_TIME_FOR_UNIX))
.build())
.addAllTokens(registrationTokens)
.build();

BatchResponse response;
try {
response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
pushSuccessValidate(registrationTokens, response);
} catch (FirebaseMessagingException e) {
e.printStackTrace();
// 예외 처리 추가 가능
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명입니다.");
Expand All @@ -54,6 +62,12 @@ private static void pushSuccessValidate(final List<String> registrationTokens, f
System.out.println(response.getSuccessCount() + " messages were sent successfully.");
if (response.getFailureCount() > 0) {
System.out.println(response.getFailureCount() + " messages failed to send.");
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());
}
});
}
}
}
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 10 14 * * ?", zone = "Asia/Seoul")
@Scheduled(cron = "0 30 15 * * ?", zone = "Asia/Seoul")
public void sendNotifications() {
LocalDate today = LocalDate.now();
List<Notification> dueNotifications = notificationRepository.findByNotificationDate(today);
Expand Down