59 lines
1.9 KiB
Java
59 lines
1.9 KiB
Java
package com.goldenchart.config;
|
|
|
|
import com.google.auth.oauth2.GoogleCredentials;
|
|
import com.google.firebase.FirebaseApp;
|
|
import com.google.firebase.FirebaseOptions;
|
|
import jakarta.annotation.PostConstruct;
|
|
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 java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
|
|
@Configuration
|
|
@Slf4j
|
|
public class FirebaseConfig {
|
|
|
|
@Value("${firebase.service-account-file:firebase-service-account.json}")
|
|
private String serviceAccountFile;
|
|
|
|
@Value("${firebase.enabled:false}")
|
|
private boolean firebaseEnabled;
|
|
|
|
@PostConstruct
|
|
public void initialize() {
|
|
if (!firebaseEnabled) {
|
|
log.info("[Firebase] disabled (firebase.enabled=false)");
|
|
return;
|
|
}
|
|
if (!FirebaseApp.getApps().isEmpty()) return;
|
|
|
|
try (InputStream in = openServiceAccount()) {
|
|
if (in == null) {
|
|
log.warn("[Firebase] service account not found — FCM disabled");
|
|
return;
|
|
}
|
|
FirebaseOptions options = FirebaseOptions.builder()
|
|
.setCredentials(GoogleCredentials.fromStream(in))
|
|
.build();
|
|
FirebaseApp.initializeApp(options);
|
|
log.info("[Firebase] Admin SDK initialized — FCM enabled");
|
|
} catch (Exception e) {
|
|
log.warn("[Firebase] init failed: {}", e.getMessage());
|
|
}
|
|
}
|
|
|
|
private InputStream openServiceAccount() {
|
|
try {
|
|
var res = new ClassPathResource(serviceAccountFile);
|
|
if (res.exists()) return res.getInputStream();
|
|
} catch (Exception ignored) { }
|
|
try {
|
|
return new FileInputStream(serviceAccountFile);
|
|
} catch (Exception ignored) { }
|
|
return null;
|
|
}
|
|
}
|