-
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.
Browse files
Browse the repository at this point in the history
Feature #113 : fcm 적용
- Loading branch information
Showing
26 changed files
with
324 additions
and
16 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
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
27 changes: 27 additions & 0 deletions
27
...src/main/java/b1nd/dodam/restapi/nightstudy/application/NightStudyPushAlarmScheduler.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,27 @@ | ||
package b1nd.dodam.restapi.nightstudy.application; | ||
|
||
import b1nd.dodam.domain.rds.nightstudy.entity.NightStudy; | ||
import b1nd.dodam.domain.rds.nightstudy.service.NightStudyService; | ||
import b1nd.dodam.firebase.client.FCMClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class NightStudyPushAlarmScheduler { | ||
private final NightStudyService service; | ||
private final FCMClient fcmClient; | ||
|
||
@Scheduled(cron = "0 0 11 * * ?") | ||
public void scheduledPushAlarm() { | ||
List<NightStudy> nightStudies = service.getByEndDate(LocalDate.now().minusDays(1)); | ||
List<String> tokens = nightStudies.stream() | ||
.map(n -> n.getStudent().getMember().getPushToken()) | ||
.toList(); | ||
fcmClient.sendMessages(tokens, "심야자습 만료", "심야자습이 만료됐어요."); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
...dodam-rest-api/src/main/java/b1nd/dodam/restapi/support/pushalarm/ApprovalAlarmEvent.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,5 @@ | ||
package b1nd.dodam.restapi.support.pushalarm; | ||
|
||
import b1nd.dodam.firebase.client.PushAlarmEvent; | ||
|
||
public record ApprovalAlarmEvent(String pushToken, String title, String body) implements PushAlarmEvent {} |
24 changes: 24 additions & 0 deletions
24
.../dodam-rest-api/src/main/java/b1nd/dodam/restapi/support/pushalarm/ApprovalAlarmUtil.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,24 @@ | ||
package b1nd.dodam.restapi.support.pushalarm; | ||
|
||
import b1nd.dodam.domain.rds.support.enumeration.ApprovalStatus; | ||
|
||
public final class ApprovalAlarmUtil { | ||
public ApprovalAlarmUtil() {} | ||
|
||
public static ApprovalAlarmEvent createAlarmEvent(String pushToken, String target, String rejectReason, ApprovalStatus status) { | ||
return new ApprovalAlarmEvent( | ||
pushToken, | ||
target, | ||
createBody(target, rejectReason, status) | ||
); | ||
} | ||
|
||
private static String createBody(String target, String rejectReason, ApprovalStatus status){ | ||
return switch (status) { | ||
|
||
case ALLOWED -> target+"이 승인됐어요!"; | ||
case REJECTED -> target+"이 거절됐어요\n"+ (rejectReason != null ? "사유: " + rejectReason : ""); | ||
case PENDING -> target+"이 승인 취소됐어요"; | ||
}; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...on/dodam-rest-api/src/main/java/b1nd/dodam/restapi/support/pushalarm/PushAlarmAspect.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,83 @@ | ||
package b1nd.dodam.restapi.support.pushalarm; | ||
|
||
import b1nd.dodam.core.exception.global.InternalServerException; | ||
import b1nd.dodam.domain.rds.member.entity.Student; | ||
import b1nd.dodam.domain.rds.nightstudy.service.NightStudyService; | ||
import b1nd.dodam.domain.rds.outgoing.service.OutGoingService; | ||
import b1nd.dodam.domain.rds.outsleeping.service.OutSleepingService; | ||
import b1nd.dodam.domain.rds.support.enumeration.ApprovalStatus; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
@Aspect | ||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class PushAlarmAspect { | ||
private final ApplicationEventPublisher eventPublisher; | ||
private final NightStudyService nightStudyService; | ||
private final OutGoingService outGoingService; | ||
private final OutSleepingService outSleepingService; | ||
|
||
@Around("@annotation(pushAlarmEvent)") | ||
public Object handlePushAlarmEvent(ProceedingJoinPoint joinPoint, PushAlarmEvent pushAlarmEvent) { | ||
|
||
Object result = proceedJoinPointSafely(joinPoint); | ||
|
||
Long id = getIdFromArgs(joinPoint.getArgs()); | ||
String rejectReason = getRejectReasonFromArgs(joinPoint.getArgs()); | ||
String target = pushAlarmEvent.target(); | ||
ApprovalStatus status = pushAlarmEvent.status(); | ||
|
||
Student student = getStudentByTargetAndId(target, id); | ||
|
||
sendPushAlarm(student, target, rejectReason, status); | ||
|
||
return result; | ||
} | ||
|
||
private Object proceedJoinPointSafely(ProceedingJoinPoint joinPoint) { | ||
try { | ||
return joinPoint.proceed(); | ||
} catch (Throwable e) { | ||
throw new InternalServerException(); | ||
} | ||
} | ||
|
||
private Long getIdFromArgs(Object[] args) { | ||
if (args[0] instanceof Long id) { | ||
return id; | ||
} else { | ||
throw new InternalServerException(); | ||
} | ||
} | ||
|
||
private String getRejectReasonFromArgs(Object[] args) { | ||
if (args.length > 1 && args[1] instanceof Optional<?> optionalArg) { | ||
return (String) optionalArg.orElse(null); | ||
} | ||
return null; | ||
} | ||
|
||
private Student getStudentByTargetAndId(String target, Long id) { | ||
return switch (target) { | ||
case "외출" -> outGoingService.getById(id).getStudent(); | ||
case "외박" -> outSleepingService.getById(id).getStudent(); | ||
case "심야자습" -> nightStudyService.getBy(id).getStudent(); | ||
default -> throw new InternalServerException(); | ||
}; | ||
} | ||
|
||
private void sendPushAlarm(Student student, String target, String rejectReason, ApprovalStatus status) { | ||
String pushToken = student.getMember().getPushToken(); | ||
ApprovalAlarmEvent alarmEvent = ApprovalAlarmUtil.createAlarmEvent(pushToken, target, rejectReason, status); | ||
eventPublisher.publishEvent(alarmEvent); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ion/dodam-rest-api/src/main/java/b1nd/dodam/restapi/support/pushalarm/PushAlarmEvent.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,15 @@ | ||
package b1nd.dodam.restapi.support.pushalarm; | ||
|
||
import b1nd.dodam.domain.rds.support.enumeration.ApprovalStatus; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface PushAlarmEvent { | ||
String target(); | ||
ApprovalStatus status(); | ||
} |
20 changes: 20 additions & 0 deletions
20
...am-rest-api/src/main/java/b1nd/dodam/restapi/support/pushalarm/PushAlarmEventHandler.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,20 @@ | ||
package b1nd.dodam.restapi.support.pushalarm; | ||
|
||
import b1nd.dodam.firebase.client.FCMClient; | ||
import b1nd.dodam.firebase.client.PushAlarmEvent; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class PushAlarmEventHandler { | ||
private final FCMClient fcmClient; | ||
|
||
@Async | ||
@TransactionalEventListener | ||
public void listen(PushAlarmEvent e){ | ||
fcmClient.sendMessage(e.pushToken(), e.title(), e.body()); | ||
} | ||
} |
Oops, something went wrong.