forked from PSMRI/Common-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirebaseMessagingConfig.java
More file actions
50 lines (37 loc) · 1.62 KB
/
FirebaseMessagingConfig.java
File metadata and controls
50 lines (37 loc) · 1.62 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
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.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.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
@Configuration
public class FirebaseMessagingConfig {
@Value("${firebase.enabled:false}")
private boolean firebaseEnabled;
@Value("${firebase.credential-file:}")
private String firebaseCredentialFile;
@Bean
public FirebaseMessaging firebaseMessaging() throws IOException {
if (!firebaseEnabled) {
throw new IllegalStateException("Firebase is disabled");
}
if (firebaseCredentialFile == null || firebaseCredentialFile.isBlank()) {
throw new IllegalStateException("No Firebase credentials path provided");
}
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(firebaseCredentialFile));
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(credentials)
.build();
FirebaseApp firebaseApp = FirebaseApp.getApps().isEmpty()
? FirebaseApp.initializeApp(options)
: FirebaseApp.getInstance();
return FirebaseMessaging.getInstance(firebaseApp);
}
}