Skip to content

Commit

Permalink
fix: modify send expiration notification logic (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 authored Sep 16, 2023
2 parents 7ebc5b7 + e5acbb5 commit 3157899
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,4 @@ public Member convertKakaoDtotoMember(KakaoDto kakaoDto) {
.username(kakaoDto.getUsername())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ public class SecurityConfig {
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(authorizeHttpRequests ->
authorizeHttpRequests.requestMatchers("/auth/sign-up", "/auth/sign-in", "/auth/sign-in/**",
"/swagger-resources/**", "/swagger-ui/**", "/v3/api-docs/**", "/webjars/**", "/error").permitAll()
authorizeHttpRequests.requestMatchers(
"/auth/sign-up",
"/auth/sign-in",
"/auth/sign-in/**",
"/swagger-resources/**",
"/swagger-ui/**",
"/v3/api-docs/**",
"/webjars/**",
"/error",
"/notifications").permitAll()
.anyRequest().authenticated())
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
.exceptionHandling(exceptionHandling -> exceptionHandling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.swmaestro.repl.gifthub.notifications.dto.DeviceTokenSaveRequestDto;
import org.swmaestro.repl.gifthub.notifications.dto.NoticeNotificationDto;
import org.swmaestro.repl.gifthub.notifications.service.FCMNotificationService;
import org.swmaestro.repl.gifthub.notifications.service.NotificationService;
import org.swmaestro.repl.gifthub.util.HttpJsonHeaders;
Expand Down Expand Up @@ -93,4 +94,21 @@ public ResponseEntity<Message> readNotification(@RequestHeader("Authorization")
HttpStatus.OK
);
}

@PostMapping
@Operation(summary = "Notification 전송 메서드", description = "모든 클라이언트에게 일괄적으로 알림을 전송하기 위한 메서드입니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "알림 전송 성공")
})
public ResponseEntity<Message> sendNotification(@RequestBody NoticeNotificationDto noticeNotificationDto) {
fcmNotificationService.sendNotification(noticeNotificationDto);
return new ResponseEntity<>(
Message.builder()
.status(StatusEnum.OK)
.message("알림 전송에 성공하였습니다!")
.build(),
new HttpJsonHeaders(),
HttpStatus.OK
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.swmaestro.repl.gifthub.notifications.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class NoticeNotificationDto {
private String title;
private String body;

@Builder
public NoticeNotificationDto(String title, String body) {
this.title = title;
this.body = body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class Notification {
private String message;

@CreatedDate
@Column(nullable = false)
private LocalDateTime createdAt;

private LocalDateTime deletedAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.swmaestro.repl.gifthub.auth.entity.DeviceToken;
import org.swmaestro.repl.gifthub.auth.service.DeviceTokenService;
import org.swmaestro.repl.gifthub.auth.service.MemberService;
import org.swmaestro.repl.gifthub.exception.BusinessException;
import org.swmaestro.repl.gifthub.notifications.NotificationType;
import org.swmaestro.repl.gifthub.notifications.dto.FCMNotificationRequestDto;
import org.swmaestro.repl.gifthub.notifications.dto.NoticeNotificationDto;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
Expand Down Expand Up @@ -50,4 +50,26 @@ public void sendNotificationByToken(FCMNotificationRequestDto requestDto) {
}
}
}

public void sendNotification(NoticeNotificationDto noticeNotificationDto) {
List<DeviceToken> deviceTokenList = deviceTokenService.list();

for (DeviceToken deviceToken : deviceTokenList) {
Notification notification = Notification.builder()
.setTitle(noticeNotificationDto.getTitle())
.setBody(noticeNotificationDto.getBody())
.build();

Message message = Message.builder()
.setToken(deviceToken.getToken())
.setNotification(notification)
.build();

try {
firebaseMessaging.send(message);
} catch (FirebaseMessagingException e) {
deviceTokenService.delete(deviceToken.getToken());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ScheduledTasks {
private final DeviceTokenService deviceTokenService;
private final FCMNotificationService fcmNotificationService;

@Scheduled(cron = "0 0 10 * * ?", zone = "Asia/Seoul") // 매일 오전 10시 실행
@Scheduled(cron = "0 35 10 * * ?", zone = "Asia/Seoul") // 매일 오전 10시 실행
public void sendExpirationNotification() {
LocalDate today = LocalDate.now();
List<Voucher> expiringVoucherList = voucherService.list().stream().filter(voucher -> {
Expand Down

0 comments on commit 3157899

Please sign in to comment.