Skip to content

Commit

Permalink
chore: 알림 로깅 추가 및 불필요한 코드 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim0914 committed Jan 22, 2024
1 parent 53c10f7 commit 397222e
Showing 1 changed file with 7 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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<FcmMessageResponse> request = new HttpEntity<>(message, headers);
//
// ResponseEntity<FcmMessageResponse> 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();
}
}

0 comments on commit 397222e

Please sign in to comment.