From 7e5aa442ff5f981c39472f16a12088d5faf82d65 Mon Sep 17 00:00:00 2001 From: Hanbee Lee Date: Wed, 24 Jan 2024 01:24:26 +0900 Subject: [PATCH] deploy: fcm --- .../example/fcm/service/FcmInitializer.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Infra/src/main/java/com/example/fcm/service/FcmInitializer.java diff --git a/Infra/src/main/java/com/example/fcm/service/FcmInitializer.java b/Infra/src/main/java/com/example/fcm/service/FcmInitializer.java new file mode 100644 index 0000000..b54a97b --- /dev/null +++ b/Infra/src/main/java/com/example/fcm/service/FcmInitializer.java @@ -0,0 +1,42 @@ +package com.example.fcm.service; + +import com.google.auth.oauth2.GoogleCredentials; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import com.google.firebase.messaging.FirebaseMessaging; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +@Configuration +public class FcmInitializer { + @Value("${app.firebase-configuration-file}") + private String firebaseConfigPath; + + @Bean + FirebaseMessaging firebaseMessaging() throws IOException { + ClassPathResource resource = new ClassPathResource(firebaseConfigPath); + InputStream resourceInputStream = resource.getInputStream(); + FirebaseApp firebaseApp = null; + List apps = FirebaseApp.getApps(); + + if (apps != null && !apps.isEmpty()) { + for (FirebaseApp app : apps) { + if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { + firebaseApp = app; + } + } + } + else { + FirebaseOptions options = FirebaseOptions.builder() + .setCredentials(GoogleCredentials.fromStream(resourceInputStream)).build(); + firebaseApp = FirebaseApp.initializeApp(options); + } + return FirebaseMessaging.getInstance(firebaseApp); + } +} \ No newline at end of file