diff --git a/src/main/java/com/oya/kr/chat/config/FCMInitializer.java b/src/main/java/com/oya/kr/chat/config/FCMInitializer.java index 40d30b0..1162bc2 100644 --- a/src/main/java/com/oya/kr/chat/config/FCMInitializer.java +++ b/src/main/java/com/oya/kr/chat/config/FCMInitializer.java @@ -4,7 +4,12 @@ import javax.annotation.PostConstruct; -import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.core.io.DefaultResourceLoader; + import org.springframework.stereotype.Service; import com.google.auth.oauth2.GoogleCredentials; @@ -21,7 +26,6 @@ @Slf4j public class FCMInitializer { - private static final String FIREBASE_CONFIG_PATH = "firebase.json"; private static boolean initialized = false; /** @@ -31,23 +35,37 @@ public class FCMInitializer { * @since 2024.03.07 */ @PostConstruct - public void initialize() { + public void initialize() throws IOException { if (!initialized) { - try { - GoogleCredentials googleCredentials = GoogleCredentials - .fromStream(new ClassPathResource(FIREBASE_CONFIG_PATH).getInputStream()); - FirebaseOptions options = new FirebaseOptions.Builder() - .setCredentials(googleCredentials) - .build(); - FirebaseApp.initializeApp(options); - initialized = true; - log.info("FCM 성공"); - } catch (IOException e) { - log.info("FCM 오류"); - log.error("FCM 오류 메시지: " + e.getMessage()); + + Resource[] resources = new PathMatchingResourcePatternResolver(new DefaultResourceLoader()) + .getResources("classpath*:firebase.json"); + + if (resources.length > 0) { + Resource resource = resources[0]; + if (!initialized) { + try { + GoogleCredentials googleCredentials = GoogleCredentials.fromStream(resource.getInputStream()); + FirebaseOptions options = new FirebaseOptions.Builder() + .setCredentials(googleCredentials) + .build(); + FirebaseApp.initializeApp(options); + initialized = true; + log.info("FCM 성공"); + } catch (IOException e) { + log.info("FCM 오류"); + log.error("FCM 오류 메시지: " + e.getMessage()); + } + } else { + log.warn("FirebaseApp 이름 [DEFAULT]으로 초기화된 앱이 이미 존재합니다."); + } + } else { + // 리소스를 찾을 수 없을 때의 처리 로직 + log.error("Resource not found: firebase.json"); } } else { log.warn("FirebaseApp 이름 [DEFAULT]으로 초기화된 앱이 이미 존재합니다."); } } + }