맞춤 케이크 예약 서비스에서는 고객이 원하는 날짜에 맞춰 케이크를 주문할 수 있도록 돕는 것이 무엇보다 중요하다고 생각했다. 특히, 사용자가 기념일을 깜빡하지 않도록 미리 알림을 제공하는 기능이 있으면 서비스 활용도가 더욱 높아질 것이라 판단했다.
이번 개발에서는 기념일 1주일 전에 사용자에게 알림을 보내는 기능을 구현했다.
이를 위해 Spring Boot의 스케줄링 기능(@Scheduled)과 Twilio SMS API를 활용하여, 사용자의 기념일을 자동으로 감지하고 사전에 알림을 보내는 시스템을 개발했다.
이제 구체적인 구현 방법을 코드와 함께 정리해보겠다.
Spring Boot: 기념일 알림 스케줄링 및 SMS 전송
1. 스케줄러 설정 및 실행
먼저, Spring Boot의 @Scheduled를 활용하여 특정 시간마다 실행되는 기능을 구현했다.
- 설정된 실행 조건: 매일 오전 9시에 기념일 알림을 자동 전송
// 매일 오전 9시에 자동 실행
@Scheduled(cron = "0 0 9 * * *")
public void sendAnniversaryNotifications() {
System.out.println("스케줄 실행");
try {
smsService.sendNotificationsForToday();
} catch (Exception e) {
System.err.println("스케줄러 작업 중 오류 발생: " + e.getMessage());
e.printStackTrace();
}
}
- @Scheduled(cron = "0 0 9 * * *") → 매일 오전 9시에 실행
- smsService.sendNotificationsForToday() 호출 → 기념일 알림을 전송하는 메서드 실행
- 예외 발생 시 로그를 출력하여 디버깅 가능하도록 설정
2. 기념일 데이터 조회 및 알림 전송
스케줄러가 실행되면 오늘이 기념일인 사용자 목록을 조회한 후, 해당 사용자에게 SMS를 전송한다.
public void sendNotificationsForToday() {
// 오늘 날짜에 알림이 설정된 기념일 조회
List<JuAnniversaryVo> notifications = juDao.getNotificationsForToday();
for (JuAnniversaryVo notification : notifications) {
// 전화번호 국제 형식 변환
String formattedPhoneNumber = formatPhoneNumber(notification.getPhone());
// 메시지 내용 생성
String message = String.format(
"안녕하세요 %s님, %s 기념일이 곧 다가옵니다! 기념일 날짜: %s",
notification.getName(),
notification.getAnniversaryName(),
notification.getAnniversaryDate()
);
// SMS 발송
boolean isSent = sendSms(formattedPhoneNumber, message);
// SMS 발송 결과 저장
juDao.insertSmsHistory(
notification.getUserId(),
notification.getAnniversaryId(),
formattedPhoneNumber,
message,
isSent ? "성공" : "실패"
);
}
}
- juDao.getNotificationsForToday() → 오늘이 기념일인 사용자 조회
- formatPhoneNumber(notification.getPhone()) → 전화번호를 국제 형식으로 변환
- sendSms() → Twilio API를 활용하여 SMS 전송
- juDao.insertSmsHistory() → 알림 발송 결과를 DB에 저장
3. SMS 전송 로직 (Twilio 활용)
Twilio API를 이용하여 SMS를 전송하는 로직은 다음과 같다.
private boolean sendSms(String phoneNumber, String message) {
try {
Message.creator(
new PhoneNumber(phoneNumber),
messagingServiceSid,
message
).create();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
- Message.creator() → Twilio를 사용하여 SMS 전송
- 성공 시 true, 실패 시 false 반환하여 알림 발송 결과를 저장 가능
마무리
이번 개발을 통해 Spring Boot의 스케줄링 기능과 Twilio API를 활용한 자동화 시스템 구축을 경험할 수 있었다.
하지만 아직 부족한 점이 있어 몇 가지 개선이 필요하다.
향후 개선 방향
- 사용자가 알림 시간을 설정할 수 있도록 기능 추가
- 기념일 알림을 이메일이나 앱 푸시 알림으로 확장
- Twilio 외에도 다양한 SMS 전송 서비스를 테스트하여 최적의 서비스 선택
'SpringBoot' 카테고리의 다른 글
[SpringBoot] 스프링부트 어노테이션(Annotation) 정리 (0) | 2025.03.05 |
---|---|
[SpringBoot]AWS S3를 활용한 파일 업로드 구현하기 (0) | 2025.02.28 |
[SpringBoot] Twilio를 활용한 React & Spring Boot 휴대폰 인증 기능 개발 (0) | 2025.02.21 |
[SpringBoot] Spring Boot + React를 활용한 OAuth 2.0 로그인 구현 (카카오 기준) (0) | 2025.02.15 |
[SpringBoot] jsp 만들기 (0) | 2025.02.10 |