FirebaseConfig.java
1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ecommerce.notification.config;
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.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import javax.annotation.PostConstruct;
import java.io.IOException;
@Slf4j
@Configuration
public class FirebaseConfig {
@Value("${fcm.service-account-file:firebase-service-account.json}")
private String serviceAccountFile;
@Value("${fcm.database-url:https://your-project-id.firebaseio.com}")
private String databaseUrl;
@PostConstruct
public void initialize() {
try {
if (FirebaseApp.getApps().isEmpty()) {
GoogleCredentials credentials = GoogleCredentials
.fromStream(new ClassPathResource(serviceAccountFile).getInputStream());
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(credentials)
.setDatabaseUrl(databaseUrl)
.build();
FirebaseApp.initializeApp(options);
log.info("Firebase app initialized successfully");
}
} catch (IOException e) {
log.error("Failed to initialize Firebase app: {}", e.getMessage());
}
}
}