-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ♻️ Refactor: FCM 알림 기능 리팩토링 * ✨ Feature: 리마인더 기능 보완
- Loading branch information
1 parent
b8149f2
commit 422c4c5
Showing
9 changed files
with
122 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...api/src/main/java/zzangdol/user/presentation/dto/request/TestPushNotificationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package zzangdol.user.presentation.dto.request; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class TestPushNotificationRequest { | ||
|
||
private String fcmToken; | ||
private String title; | ||
private String body; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
moodoodle-domain/src/main/java/zzangdol/user/dao/querydsl/UserQueryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package zzangdol.user.dao.querydsl; | ||
|
||
import java.util.List; | ||
import zzangdol.user.domain.User; | ||
|
||
public interface UserQueryRepository { | ||
|
||
List<User> findUsersWithNotificationTime(int hour, int minute); | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
moodoodle-domain/src/main/java/zzangdol/user/dao/querydsl/UserQueryRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package zzangdol.user.dao.querydsl; | ||
|
||
import static zzangdol.user.domain.QUser.user; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import zzangdol.user.domain.User; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class UserQueryRepositoryImpl implements UserQueryRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public List<User> findUsersWithNotificationTime(int hour, int minute) { | ||
return queryFactory.selectFrom(user) | ||
.where(user.notificationTime.hour().eq(hour) | ||
.and(user.notificationTime.minute().eq(minute))) | ||
.fetch(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 50 additions & 5 deletions
55
moodoodle-notification/src/main/java/zzangdol/NotificationScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,68 @@ | ||
package zzangdol; | ||
|
||
import java.time.LocalTime; | ||
import java.util.List; | ||
import java.util.Random; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import zzangdol.notification.domain.FcmToken; | ||
import zzangdol.user.dao.UserRepository; | ||
import zzangdol.user.domain.User; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class NotificationScheduler { | ||
|
||
private final FCMService fcmService; | ||
private final UserRepository userRepository; | ||
|
||
@Scheduled(cron = "0 0 21 * * ?") | ||
private static final List<String> TITLES = List.of( | ||
"무두들: 오늘의 일기 시간이에요!", | ||
"무두들: 일기 쓸 시간입니다", | ||
"무두들: 오늘 하루 어땠나요?", | ||
"무두들: 일기 작성 시간!", | ||
"무두들: 오늘의 생각을 기록해보세요", | ||
"무두들: 하루를 마무리하며 일기를 써보세요", | ||
"무두들: 당신의 하루를 기록하세요", | ||
"무두들: 일기 쓰는 시간을 잊지 마세요", | ||
"무두들: 오늘의 일기를 써보세요", | ||
"무두들: 잠시 시간을 내어 일기를 써보세요" | ||
); | ||
|
||
private static final List<String> BODIES = List.of( | ||
"무두들에서 오늘 하루 있었던 일들을 일기에 적어보세요.", | ||
"무두들에 오늘의 감정과 경험을 남겨보세요.", | ||
"무두들에서 일기를 쓰며 하루를 정리해보세요.", | ||
"무두들에 오늘 하루를 돌아보며 일기를 작성해보세요.", | ||
"무두들에서 일기를 통해 하루를 마무리해보세요.", | ||
"무두들에 오늘 느낀 감정과 생각을 기록해보세요.", | ||
"무두들에서 일기를 쓰며 하루를 정리해보는 건 어떨까요?", | ||
"무두들에 오늘의 소중한 순간들을 남겨보세요.", | ||
"무두들에서 하루를 마무리하며 일기를 써보세요.", | ||
"무두들에 오늘 하루를 기록하며 내일을 준비해보세요." | ||
); | ||
|
||
@Scheduled(cron = "0,30 * * * * ?") // 30분 단위로 실행 | ||
public void scheduleDailyNotification() { | ||
try { | ||
String title = "Daily Reminder"; | ||
String body = "This is your daily reminder!"; | ||
fcmService.sendNotificationToAllUsers(title, body); | ||
LocalTime now = LocalTime.now(); | ||
int currentHour = now.getHour(); | ||
int currentMinute = now.getMinute(); | ||
|
||
List<User> users = userRepository.findUsersWithNotificationTime(currentHour, currentMinute); | ||
|
||
for (User user : users) { | ||
Random random = new Random(); | ||
String title = TITLES.get(random.nextInt(TITLES.size())); | ||
String body = BODIES.get(random.nextInt(BODIES.size())); | ||
List<FcmToken> tokens = user.getFcmTokens(); | ||
for (FcmToken token : tokens) { | ||
fcmService.sendNotification(token.getToken(), title, body); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |