-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationScheduler.java
More file actions
151 lines (123 loc) · 5.88 KB
/
NotificationScheduler.java
File metadata and controls
151 lines (123 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.challenge.scheduler;
import com.challenge.api.service.fcm.FcmService;
import com.challenge.api.service.fcm.request.FcmMessage;
import com.challenge.api.service.notification.AchieveChallengeDTO;
import com.challenge.api.service.notification.NewChallengeDTO;
import com.challenge.api.service.notification.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Profile("dev")
@Slf4j
@Component
@RequiredArgsConstructor
public class NotificationScheduler {
private final NotificationService notificationService;
private final FcmService fcmService;
private static final String NEW_CHALLENGE_TITLE = "⛳️ 챌린지를 등록해 보세요!";
private static final String ACHIEVE_DAY_START_TITLE = "📬 오늘 달성할 수 있는 챌린지";
private static final String ACHIEVE_DAY_END_TITLE = "🙌 달성할 수 있는 챌린지가 있어요!";
private static final String NEW_CHALLENGE_BODY = "님, 지금 새 챌린지를 등록하고 달성할 수 있어요";
private static final String ACHIEVE_DAY_START_BODY = "포함 %d개의 챌린지";
private static final String ACHIEVE_DAY_END_BODY = "님, 아직 달성할 수 있는 챌린지가 %d개 있어요!";
private static final int MAX_TITLES = 3;
/**
* 새로운 챌린지 등록 알림 발송
*/
@Scheduled(cron = "0 0 9 * * *")
public void sendNewChallengeNotification() {
try {
// 알림 전송 대상 조회
Map<String, NewChallengeDTO> targetMap = notificationService.getNewChallengeTargets();
// 알림 객체 생성
List<FcmMessage> fcmMessages = targetMap.entrySet().stream()
.map(entry ->
FcmMessage.of(entry.getKey(), NEW_CHALLENGE_TITLE,
entry.getValue().getNickname() + NEW_CHALLENGE_BODY)
).toList();
// 알림 발송 및 내역 저장
fcmMessages.forEach(message -> {
// 알림 발송
fcmService.sendMessage(message);
// 알림 내역 저장
Long memberId = targetMap.get(message.getToken()).getMemberId();
notificationService.createAndSave(memberId, message.getTitle(), message.getBody());
log.debug("New challenge notification sent: {}", message);
});
} catch (Exception e) {
log.error("error occuerd while sending new challenge notification", e);
}
}
/**
* 챌린지 하루 시작 알림 발송
*/
@Scheduled(cron = "0 0 9 * * *")
public void sendDayStartNotification() {
try {
// 알림 전송 대상 조회
Map<String, AchieveChallengeDTO> targetMap = notificationService.getAchieveTargetsAndChallenge();
// 알림 객체 생성
List<FcmMessage> fcmMessages = targetMap.entrySet().stream()
.map(entry -> FcmMessage.of(entry.getKey(), ACHIEVE_DAY_START_TITLE,
getDayStartNotificationBody(entry.getValue()))
).toList();
// 알림 발송 및 내역 저장
fcmMessages.forEach(message -> {
// 알림 발송
fcmService.sendMessage(message);
// 알림 내역 저장
Long memberId = targetMap.get(message.getToken()).getMemberId();
notificationService.createAndSave(memberId, message.getTitle(), message.getBody());
log.debug("Day start notification sent: {}", message);
});
} catch (Exception e) {
log.error("error occuerd while sending day start notification", e);
}
}
/**
* 챌린지 하루 종료 알림 발송
*/
@Scheduled(cron = "0 0 21 * * *")
public void sendDayEndNotification() {
try {
// 알림 전송 대상 조회
Map<String, AchieveChallengeDTO> targetMap = notificationService.getAchieveTargetsAndChallenge();
// 알림 객체 생성
List<FcmMessage> fcmMessages = targetMap.entrySet().stream()
.map(entry ->
FcmMessage.of(entry.getKey(), ACHIEVE_DAY_END_TITLE,
getDayEndNotificationBody(entry.getValue()))
).toList();
// 알림 발송 및 내역 저장
fcmMessages.forEach(message -> {
// 알림 발송
fcmService.sendMessage(message);
// 알림 내역 저장
Long memberId = targetMap.get(message.getToken()).getMemberId();
notificationService.createAndSave(memberId, message.getTitle(), message.getBody());
log.debug("Day end notification sent: {}", message);
});
} catch (Exception e) {
log.error("error occuerd while sending day end notification", e);
}
}
private String getDayStartNotificationBody(AchieveChallengeDTO dto) {
List<String> challengeTitles = dto.getChallengeTitles();
String body = challengeTitles.stream()
.map(title -> "- " + title)
.collect(Collectors.joining("\n"));
// 타이틀 개수가 3 이상인 경우 메시지 추가
if (challengeTitles.size() >= MAX_TITLES) {
body += "\n" + String.format(ACHIEVE_DAY_START_BODY, challengeTitles.size());
}
return body;
}
private String getDayEndNotificationBody(AchieveChallengeDTO dto) {
return dto.getNickname() + String.format(ACHIEVE_DAY_END_BODY, dto.getChallengeTitles().size());
}
}