-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 알림을 차단하고, 허용했을 때 새로운 FCM토큰이 발급되는데 이때, 전 토큰값이 그대로 DB에 저장되어 FCM 웹푸시가 되지 않음 - 새로운 토큰이 들어왔을 때, 전 토큰값을 삭제하고 새로운 토큰을 저장 리팩토링 - FcmToken, FCMAlarm 서비스 분리 - 토큰 생성/수정/삭제 로직과 웹푸시 알람 보내는 메서드 분리 - FcmToken Entity 연관관계 수정 - @OnetoOne -> @manytoone로 수정하여 User를 FK로 설정 - 고유의 id값을 PK로 설정 - ScheduledService 내용 수정 - 운동일지가 비었을 때, 비어있다는 알림 추가 - NotificationApiController 삭제, FcmTokenController 로 변경
- Loading branch information
Showing
8 changed files
with
149 additions
and
88 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/com/ogjg/daitgym/alarm/controller/fcm/FcmTokenController.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,39 @@ | ||
package com.ogjg.daitgym.alarm.controller.fcm; | ||
|
||
import com.ogjg.daitgym.alarm.dto.FcmTokenRequestDto; | ||
import com.ogjg.daitgym.alarm.service.FcmTokenService; | ||
import com.ogjg.daitgym.common.exception.ErrorCode; | ||
import com.ogjg.daitgym.common.response.ApiResponse; | ||
import com.ogjg.daitgym.config.security.details.OAuth2JwtUserDetails; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/notification") | ||
public class FcmTokenController { | ||
|
||
private final FcmTokenService fcmTokenService; | ||
|
||
|
||
@GetMapping("/token") | ||
public ApiResponse<Boolean> getNotification(@AuthenticationPrincipal OAuth2JwtUserDetails oAuth2JwtUserDetails) { | ||
|
||
return new ApiResponse<>(ErrorCode.SUCCESS, fcmTokenService.getNotification(oAuth2JwtUserDetails)); | ||
} | ||
|
||
@PostMapping("/token") | ||
public ApiResponse<Void> saveNotification(@RequestBody FcmTokenRequestDto fcmTokenRequestDto, | ||
@AuthenticationPrincipal OAuth2JwtUserDetails oAuth2JwtUserDetails) { | ||
fcmTokenService.saveNotification(fcmTokenRequestDto, oAuth2JwtUserDetails); | ||
return new ApiResponse<>(ErrorCode.SUCCESS); | ||
} | ||
|
||
@PatchMapping("/token") | ||
public ApiResponse<Void> updateNotification(@RequestBody FcmTokenRequestDto fcmTokenRequestDto, | ||
@AuthenticationPrincipal OAuth2JwtUserDetails oAuth2JwtUserDetails) { | ||
fcmTokenService.updateNotification(fcmTokenRequestDto, oAuth2JwtUserDetails); | ||
return new ApiResponse<>(ErrorCode.SUCCESS); | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
src/main/java/com/ogjg/daitgym/alarm/controller/fcm/NotificationApiController.java
This file was deleted.
Oops, something went wrong.
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
71 changes: 71 additions & 0 deletions
71
src/main/java/com/ogjg/daitgym/alarm/service/FcmTokenService.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,71 @@ | ||
package com.ogjg.daitgym.alarm.service; | ||
|
||
import com.ogjg.daitgym.alarm.dto.FcmTokenRequestDto; | ||
import com.ogjg.daitgym.alarm.repository.FcmTokenRepository; | ||
import com.ogjg.daitgym.common.exception.fcmtoken.NotFoundFcmToken; | ||
import com.ogjg.daitgym.common.exception.user.NotFoundUser; | ||
import com.ogjg.daitgym.config.security.details.OAuth2JwtUserDetails; | ||
import com.ogjg.daitgym.domain.FcmToken; | ||
import com.ogjg.daitgym.domain.User; | ||
import com.ogjg.daitgym.user.service.UserHelper; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class FcmTokenService { | ||
|
||
private final UserHelper userHelper; | ||
private final FcmTokenRepository notificationRepository; | ||
private final FcmTokenRepository fcmTokenRepository; | ||
|
||
|
||
public boolean getNotification(OAuth2JwtUserDetails oAuth2JwtUserDetails) { | ||
|
||
return fcmTokenRepository.findByUserEmail(oAuth2JwtUserDetails.getEmail()).isPresent(); | ||
} | ||
|
||
@Transactional | ||
public void updateNotification(FcmTokenRequestDto fcmTokenRequestDto, OAuth2JwtUserDetails oAuth2JwtUserDetails) { | ||
User user = userHelper.findUserByEmail(oAuth2JwtUserDetails.getEmail()); | ||
String token = fcmTokenRequestDto.getToken(); | ||
|
||
FcmToken fcmToken = fcmTokenRepository.findByUser(user).orElseThrow(NotFoundUser::new); | ||
fcmToken.update(token); | ||
fcmTokenRepository.save(fcmToken); | ||
} | ||
|
||
@Transactional | ||
public void saveNotification(FcmTokenRequestDto fcmTokenRequestDto, OAuth2JwtUserDetails oAuth2JwtUserDetails) { | ||
|
||
try { | ||
User user = userHelper.findUserByEmail(oAuth2JwtUserDetails.getEmail()); | ||
String token = fcmTokenRequestDto.getToken(); | ||
|
||
if (!fcmTokenRepository.existsByUserAndToken(user, token)) { | ||
FcmToken notification = FcmToken.builder() | ||
.token(token) | ||
.user(user) | ||
.build(); | ||
notificationRepository.save(notification); | ||
log.info("토큰 저장 성공"); | ||
} else { | ||
log.info("이미 토큰이 존재합니다."); | ||
} | ||
} catch (Exception e) { | ||
log.error("트랜잭션 롤백 발생", e); | ||
throw e; | ||
} | ||
} | ||
|
||
public void deleteFcmToken(FcmTokenRequestDto fcmTokenRequestDto, OAuth2JwtUserDetails oAuth2JwtUserDetails) { | ||
User user = userHelper.findUserByEmail(oAuth2JwtUserDetails.getEmail()); | ||
String token = fcmTokenRequestDto.getToken(); | ||
|
||
FcmToken fcmToken = fcmTokenRepository.findByUserAndToken(user, token).orElseThrow(NotFoundFcmToken::new); | ||
fcmTokenRepository.delete(fcmToken); | ||
} | ||
} |
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