diff --git a/src/main/java/com/tickettogether/global/fcm/FCMInitializer.java b/src/main/java/com/tickettogether/global/fcm/FCMInitializer.java new file mode 100644 index 0000000..315725a --- /dev/null +++ b/src/main/java/com/tickettogether/global/fcm/FCMInitializer.java @@ -0,0 +1,39 @@ +package com.tickettogether.global.fcm; + +import com.google.auth.oauth2.GoogleCredentials; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.io.InputStream; + +@Component +@Slf4j +public class FCMInitializer { + + @Value("${fcm.certification}") + private String googleApplicationCredentials; + + @PostConstruct + public void initialize() throws IOException { + ClassPathResource resource = new ClassPathResource(googleApplicationCredentials); + try (InputStream is = resource.getInputStream()){ + FirebaseOptions options = FirebaseOptions.builder() + .setCredentials(GoogleCredentials.fromStream(is)) + .build(); + + if (FirebaseApp.getApps().isEmpty()) { + FirebaseApp.initializeApp(options); + log.info("FirebaseApp initialization complete"); + } + }catch(Exception ex){ + log.info(ex.getMessage()); + } + } +} +