diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index ee5e93c7..e47542a0 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -7,54 +7,35 @@ import com.google.firebase.messaging.Message; import com.google.firebase.messaging.Notification; import com.official.pium.notification.application.MessageSendManager; -import com.official.pium.notification.fcm.dto.FcmMessageResponse; import jakarta.annotation.PostConstruct; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.concurrent.ExecutionException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; @Slf4j @Component @RequiredArgsConstructor public class FcmMessageSender implements MessageSendManager { - private static final String SYSTEM_PATH = System.getProperty("user.dir"); - private static final String JSON_FILE_PATH = "/src/main/resources/config/pium-fcm.json"; - - @Value("${fcm.api.url}") - private String apiUrl; - - @Value("${fcm.key.path}") - private String keyPath; - - @Value("${fcm.key.scope}") - private String keyScope; - - private final RestTemplate restTemplate; - @PostConstruct public void initialize() { - FileInputStream serviceAccount; try { - serviceAccount = new FileInputStream(SYSTEM_PATH + JSON_FILE_PATH); + ClassPathResource resource = new ClassPathResource("config/pium-fcm.json"); FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(GoogleCredentials.fromStream(serviceAccount)) + .setCredentials(GoogleCredentials.fromStream(resource.getInputStream())) .build(); if (FirebaseApp.getApps().isEmpty()) { FirebaseApp.initializeApp(options); } } catch (FileNotFoundException e) { - log.error("파일을 찾을 수 없습니다. " + e); + log.error("파일을 찾을 수 없습니다. ", e); } catch (IOException e) { - log.error("FCM 인증이 실패했습니다. " + e); + log.error("FCM 인증이 실패했습니다. ", e); } } @@ -69,59 +50,11 @@ public void sendMessageTo(String targetToken, String title, String body) { .build(); try { String response = FirebaseMessaging.getInstance().sendAsync(message).get(); - log.info("응답 결과 : " + response); + log.info("알림 전송 성공 : " + response); } catch (InterruptedException e) { - log.error(e.getMessage()); + log.error("FCM 알림 스레드에서 문제가 발생했습니다.", e); } catch (ExecutionException e) { - log.error(e.getMessage()); + log.error("FCM 알림 전송에 실패했습니다.", e); } } - -// public void sendMessageTo(String targetToken, String title, String body) { -// try { -// FcmMessageResponse message = makeMessage(targetToken, title, body); -// -// HttpHeaders headers = new HttpHeaders(); -// headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken()); -// headers.set(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8"); -// -// HttpEntity request = new HttpEntity<>(message, headers); -// -// ResponseEntity postResult = restTemplate.postForEntity( -// apiUrl, -// request, -// FcmMessageResponse.class -// ); -// -// log.info("FCM 메시지 전송 성공: {}", postResult.getBody()); -// -// } catch (Exception e) { -// log.error("FCM 메시지 전송 실패", e); -// throw new FcmException.FcmMessageSendException(e.getMessage()); -// } -// } - - private FcmMessageResponse makeMessage(String targetToken, String title, String body) { - return FcmMessageResponse.builder() - .message(FcmMessageResponse.Message.builder() - .token(targetToken) - .notification(FcmMessageResponse.Notification.builder() - .title(title) - .body(body) - .image(null) - .build() - ) - .build() - ) - .validate_only(false) - .build(); - } - - private String getAccessToken() throws IOException { - GoogleCredentials googleCredentials = GoogleCredentials - .fromStream(new ClassPathResource(keyPath).getInputStream()) - .createScoped(keyScope); - googleCredentials.refreshIfExpired(); - return googleCredentials.getAccessToken().getTokenValue(); - } }