|
6 | 6 | import com.google.firebase.messaging.FirebaseMessaging; |
7 | 7 | import java.io.IOException; |
8 | 8 | import java.io.InputStream; |
| 9 | + |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
9 | 11 | import org.springframework.context.annotation.Bean; |
10 | 12 | import org.springframework.context.annotation.Configuration; |
11 | | -import org.springframework.core.io.FileSystemResource; |
| 13 | +import org.springframework.core.io.ClassPathResource; |
12 | 14 | import org.springframework.core.io.Resource; |
13 | 15 |
|
14 | 16 | @Configuration |
15 | 17 | public class FCMConfig { |
| 18 | + @Value("${fcm.service-account-file}") |
| 19 | + private String firebaseKeyPath; |
16 | 20 |
|
17 | 21 | @Bean |
18 | 22 | public FirebaseApp firebaseApp() throws IOException { |
19 | | - // 프로젝트 파일과 동일한 디렉토리에 있는 firebase_service_key.json 파일 경로 |
20 | | - String filePath = "./firebase_service_key.json"; |
21 | 23 |
|
22 | | - // FileSystemResource를 사용하여 파일 시스템에서 리소스를 로드 |
23 | | - Resource resource = new FileSystemResource(filePath); |
| 24 | + Resource resource = new ClassPathResource(firebaseKeyPath.replace("file:", " ")); |
24 | 25 | InputStream serviceAccount = resource.getInputStream(); |
25 | 26 |
|
26 | 27 | FirebaseOptions options = FirebaseOptions.builder() |
27 | 28 | .setCredentials(GoogleCredentials.fromStream(serviceAccount)) |
28 | 29 | .build(); |
29 | 30 |
|
30 | | - return FirebaseApp.initializeApp(options); |
| 31 | + // 이미 초기화되었는지 확인하여 중복 초기화를 방지 |
| 32 | + if (FirebaseApp.getApps().isEmpty()) { |
| 33 | + return FirebaseApp.initializeApp(options); |
| 34 | + } else { |
| 35 | + return FirebaseApp.getInstance(); |
| 36 | + } |
31 | 37 | } |
32 | 38 |
|
33 | 39 | @Bean |
|
0 commit comments