-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathFirebaseMessagingConfig.java
More file actions
65 lines (51 loc) · 2.25 KB
/
FirebaseMessagingConfig.java
File metadata and controls
65 lines (51 loc) · 2.25 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.iemr.common.config.firebase;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
@Configuration
public class FirebaseMessagingConfig {
private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
@Value("${firebase.enabled:false}")
private boolean firebaseEnabled;
@Value("${firebase.credential-file:}")
private String firebaseCredentialFile;
@Bean
@ConditionalOnProperty(name = "firebase.enabled", havingValue = "true")
public FirebaseMessaging firebaseMessaging() throws IOException {
if (!firebaseEnabled) {
logger.error("⚠️ Firebase disabled by config");
return null;
}
try {
if (firebaseCredentialFile == null || firebaseCredentialFile.isBlank()) {
logger.error("⚠️ No Firebase credentials path provided");
return null; // don't throw, app will still start
}
GoogleCredentials credentials = GoogleCredentials.fromStream(
new ClassPathResource(firebaseCredentialFile).getInputStream()
);
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(credentials)
.build();
FirebaseApp firebaseApp = FirebaseApp.getApps().isEmpty()
? FirebaseApp.initializeApp(options)
: FirebaseApp.getInstance();
return FirebaseMessaging.getInstance(firebaseApp);
} catch (Exception e) {
logger.error("⚠️ Firebase init failed: " + e.getMessage());
return null; // keep app running
}
}
}