Repick-official / repick-server-v2

🌏 지속가능한 패션문화, Repick 🌏
3 stars 0 forks source link

Feature: push notification service #132

Closed mushroom1324 closed 2 months ago

mushroom1324 commented 2 months ago

Closes #131

환경 구성

image

푸시알림을 전송하는 Lambda 함수를 사용했습니다.

자세한 정보는 테라폼 레포지토리에서 확인하실 수 있습니다.

checkNotProcessedBagInit

    @Scheduled(cron = "0 0 5 * * *", zone = "Asia/Seoul")
    public void checkNotProcessedBagInit() {
        List<BagCollect> bagCollects = bagCollectRepository.findNotProcessedBagCollects();

        bagCollects.forEach(bagCollect -> {
            int currentNotifyCount = bagCollect.getNotifyCount() + 1;

            if (currentNotifyCount == 1) {
                pushNotificationService.sendPushNotification(bagCollect.getUser().getId(), "리픽백 수거를 진행해 보세요!", "지금 바로 진행해 볼까요?");
            } else if (currentNotifyCount == 7) {
                currentNotifyCount = 0;
            }

            bagCollect.updateNotifyCount(currentNotifyCount);

        });
    }

'수거 미진행 시 푸시 (리픽백 신청일 기점으로 7일 이내 진행 없을 시)' 를 구현합니다.

PushNotificationService

private final UserFcmTokenInfoRepository userFcmTokenInfoRepository;

    public void sendPushNotification(Long userId, String title, String body) {

        LambdaClient awsLambda = LambdaClient.builder()
                .region(Region.AP_NORTHEAST_2)
                .build();

        UserFcmTokenInfo userFcmTokenInfo = userFcmTokenInfoRepository.findById(userId)
                .orElseThrow(() -> new CustomException(USER_NOT_FOUND));

        String json = String.format("{\"fcmToken\": \"%s\", \"title\": \"%s\", \"body\": \"%s\"}", userFcmTokenInfo.getFcmToken(), title, body);

        SdkBytes payload = SdkBytes.fromUtf8String(json);

        InvokeRequest request = InvokeRequest.builder()
                .functionName("lambda_push_notification_single")
                .payload(payload)
                .build();

        awsLambda.invoke(request);

    }

푸시 알림을 전송하는 서비스입니다. AWS Lambda 함수를 호출합니다.