Skip to content

Commit

Permalink
refactor: 환율 알림 1분마다 체크
Browse files Browse the repository at this point in the history
  • Loading branch information
ny2060 committed Jun 11, 2024
1 parent 367c26c commit 366c872
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ public FirebaseApp firebaseApp() throws IOException {
byte[] jsonBytes = resource.readAllBytes();
String jsonString = new String(jsonBytes, StandardCharsets.UTF_8);

// Gson을 사용하여 JSON 문자열을 JsonObject로 파싱
Gson gson = new Gson();
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();

// JsonObject를 다시 문자열로 변환
String jsonFormattedString = gson.toJson(jsonObject);

// 문자열을 다시 InputStream으로 변환
InputStream jsonInputStream = new ByteArrayInputStream(jsonFormattedString.getBytes(StandardCharsets.UTF_8));


FirebaseOptions options = FirebaseOptions
.builder()
.setCredentials(GoogleCredentials.fromStream(jsonInputStream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,10 @@ private String makeMessage(FcmSendDto fcmSendDto) throws JsonProcessingException
// }
//
// }
public BaseResponse notificationAlarm(String title, String body, DuesAlarmRequestDto duesAlarmRequestDto) throws IOException, FirebaseMessagingException {

//firebaseCreateOption();
public BaseResponse notificationAlarm(String title, String body, DuesAlarmRequestDto duesAlarmRequestDto) {

List<String> tokenList = new ArrayList<>();
//모임원 fcm token 가져오기
for (DuesAlarmRequestDto.RequestMemberInfo memberInfo:duesAlarmRequestDto.getMemberInfos()) {
Member member = memberRepository.findById(memberInfo.getMemberIdx()).orElseThrow(()->new ApiException(ExceptionEnum.MEMBER_NOT_FOUND));
tokenList.add(member.getFcmToken());
Expand All @@ -158,10 +157,11 @@ public BaseResponse notificationAlarm(String title, String body, DuesAlarmReques
.build();

try {
// sendEachForMulticast를 이용해서 다중 사용자에게 동시 알람
BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
System.out.println("FCMsendsuccess-"+response);
System.out.println("FCM success-"+response);
} catch (FirebaseMessagingException e) {
System.out.println("FCMsend-"+e.getMessage());
System.out.println("FCM error-"+e.getMessage());
}

//BatchResponse response = FirebaseMessaging.getInstance().sendEachForMulticast(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public Tasklet tasklet() {

List<ExchangeDto> exchangeDtoList = exchangeUtils.getExchangeDataAsDtoList();

// 10분마다 실시간 환율 정보 db에 저장
for(ExchangeDto exchangeDto: exchangeDtoList){
exchangeService.saveExchangeRate(exchangeDto.getCur_unit(),exchangeDto.getDeal_bas_r(),exchangeDto.getCur_nm());

}

exchangeService.checkNotifyAlarms();
exchangeService.checkNotifyAlarms(); //환율 푸시알림
return RepeatStatus.FINISHED;
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void resetNotifiedFlags() {
exchangeService.resetNotifiedFlags();
}

//@Scheduled(cron = "0/10 * * * * *")
@Scheduled(cron = "0 * * * * *")
public void run() throws Exception {
JobParameters parameters = new JobParametersBuilder()
.addString("jobName","exchangeJob"+System.currentTimeMillis())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void setExchangeRateAlarm(ExchangeRateAlarmRequestDto requestDto){
}

@Transactional
public void checkNotifyAlarms() throws IOException {
public synchronized void checkNotifyAlarms() throws IOException {

List<ExchangeRateAlarm> alarms = exchangeRateAlarmRepository.findAll();

Expand All @@ -92,18 +92,19 @@ public void checkNotifyAlarms() throws IOException {
BigDecimal currRate = exchangeRate.getCurRate();
if(alarm.getExchangeRate().getCurCode().equals(exchangeRate.getCurCode())){
boolean notify = false;
Member member = memberRepository.findById(alarm.getMember().getMemberIdx()).orElseThrow(EntityNotFoundException::new);

if (alarm.getRateType() == ExchangeRateAlarmType.OVER && currRate.compareTo(alarm.getCurRate()) >= 0) {
notify = true;
firebaseFCMService.sendMessageTo(FcmSendDto.builder().token(member.getFcmToken()).title("환율 알림").body("환율이 "+alarm.getCurRate()+" 이상에 도달했어요!").build());
} else if (alarm.getRateType() == ExchangeRateAlarmType.LESS && currRate.compareTo(alarm.getCurRate()) <= 0) {
notify = true;
}
firebaseFCMService.sendMessageTo(FcmSendDto.builder().token(member.getFcmToken()).title("환율 알림").body("환율이 "+alarm.getCurRate()+" 이하에 도달했어요!").build());

}
if (notify) {
Member member = memberRepository.findById(alarm.getMember().getMemberIdx()).orElseThrow(EntityNotFoundException::new);
firebaseFCMService.sendMessageTo(FcmSendDto.builder().token(member.getFcmToken()).title("환율 알림").body("환율이 "+alarm.getCurRate()+" 에 도달했어요~!!.").build());
alarm.setNotified(true);
exchangeRateAlarmRepository.save(alarm);

}
}

Expand Down

0 comments on commit 366c872

Please sign in to comment.