diff --git a/.gitignore b/.gitignore index 2e8dd566..61564946 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,7 @@ .idea/**/shelf # Firebase key file -firebase +**/bangfer-firebase-key.json # AWS User-specific .idea/**/aws.xml diff --git a/src/main/java/com/capstone/BnagFer/domain/firebase/controller/FcmController.java b/src/main/java/com/capstone/BnagFer/domain/firebase/controller/FcmController.java new file mode 100644 index 00000000..70c15f1e --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/firebase/controller/FcmController.java @@ -0,0 +1,27 @@ +package com.capstone.BnagFer.domain.firebase.controller; + +import com.capstone.BnagFer.domain.firebase.dto.FCMAlarmRequestDto; +import com.capstone.BnagFer.domain.firebase.service.FcmAlarmService; +import com.capstone.BnagFer.global.common.ApiResponse; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +@Slf4j +@RestController +@RequiredArgsConstructor +@Tag(name = "푸시 알림 API") +@RequestMapping("/fcm") +public class FcmController { + + private final FcmAlarmService fcmAlarmService; + + @Operation(summary = "푸시 알림 보내기", description = "해당 유저에게 FCM 푸시 알림 전송") + @PostMapping("/send/{userId}") + public ApiResponse sendAlarm(@Valid @RequestBody FCMAlarmRequestDto requestDto, @PathVariable Long userId) { + return ApiResponse.onSuccess(fcmAlarmService.sendAlarm(requestDto, userId)); + } +} \ No newline at end of file diff --git a/src/main/java/com/capstone/BnagFer/domain/firebase/dto/FCMAlarmRequestDto.java b/src/main/java/com/capstone/BnagFer/domain/firebase/dto/FCMAlarmRequestDto.java new file mode 100644 index 00000000..cbad19ad --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/firebase/dto/FCMAlarmRequestDto.java @@ -0,0 +1,7 @@ +package com.capstone.BnagFer.domain.firebase.dto; + +public record FCMAlarmRequestDto ( + String title, + String body +) { +} \ No newline at end of file diff --git a/src/main/java/com/capstone/BnagFer/domain/firebase/exception/FirebaseExceptionHandler.java b/src/main/java/com/capstone/BnagFer/domain/firebase/exception/FirebaseExceptionHandler.java new file mode 100644 index 00000000..bd0ce5ca --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/firebase/exception/FirebaseExceptionHandler.java @@ -0,0 +1,10 @@ +package com.capstone.BnagFer.domain.firebase.exception; + +import com.capstone.BnagFer.global.common.BaseErrorCode; +import com.capstone.BnagFer.global.common.exception.CustomException; + +public class FirebaseExceptionHandler extends CustomException { + public FirebaseExceptionHandler(BaseErrorCode errorCode) { + super(errorCode); + } +} \ No newline at end of file diff --git a/src/main/java/com/capstone/BnagFer/domain/firebase/service/FcmAlarmService.java b/src/main/java/com/capstone/BnagFer/domain/firebase/service/FcmAlarmService.java new file mode 100644 index 00000000..2cf80cc5 --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/firebase/service/FcmAlarmService.java @@ -0,0 +1,51 @@ +package com.capstone.BnagFer.domain.firebase.service; + +import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.global.util.RedisUtil; +import com.capstone.BnagFer.domain.accounts.repository.UserJpaRepository; +import com.capstone.BnagFer.domain.firebase.dto.FCMAlarmRequestDto; +import com.capstone.BnagFer.domain.firebase.exception.FirebaseExceptionHandler; +import com.capstone.BnagFer.global.common.ErrorCode; +import com.google.firebase.messaging.FirebaseMessaging; +import com.google.firebase.messaging.FirebaseMessagingException; +import com.google.firebase.messaging.Message; +import com.google.firebase.messaging.Notification; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class FcmAlarmService { + + private final FirebaseMessaging firebaseMessaging; + private final UserJpaRepository userJpaRepository; + private final RedisUtil redisUtil; + + public String sendAlarm(FCMAlarmRequestDto requestDto, Long userId) { + User user = userJpaRepository.findById(userId) + .orElseThrow(() -> new FirebaseExceptionHandler(ErrorCode.USER_NOT_FOUND)); + + String fcmToken = redisUtil.getFCMToken(user.getEmail()); + if (fcmToken != null) { + Notification notification = Notification.builder() + .setTitle(requestDto.title()) + .setBody(requestDto.body()) + .build(); + + Message message = Message.builder() + .setToken(fcmToken) + .setNotification(notification) + .build(); + + try { + firebaseMessaging.send(message); + return "알림을 성공적으로 전송했습니다. targetUserId = " + userId; + } catch (FirebaseMessagingException e) { + e.printStackTrace(); + throw new FirebaseExceptionHandler(ErrorCode.FIREBASE_MESSAGING_ERROR); + } + } else { + throw new FirebaseExceptionHandler(ErrorCode.FIREBASE_TOKEN_NOT_FOUND); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/capstone/BnagFer/domain/myteam/service/TeamQueryService.java b/src/main/java/com/capstone/BnagFer/domain/myteam/service/TeamQueryService.java index 12e8939e..3d018501 100644 --- a/src/main/java/com/capstone/BnagFer/domain/myteam/service/TeamQueryService.java +++ b/src/main/java/com/capstone/BnagFer/domain/myteam/service/TeamQueryService.java @@ -44,7 +44,7 @@ public List getMyTeamList(User user) { for (TeamMember teamMember : teamMembers) { Team team = teamMember.getTeam(); - teamLists.add(GetTeamResponseDto.TeamList.from(team, teamMember)); + teamLists.add(GetTeamResponseDto.TeamList.from(team)); } return teamLists;