Skip to content

Commit

Permalink
loading fcm credentials dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
azhamn committed Mar 23, 2020
1 parent dd7859f commit 7f85413
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/main/java/lk/gov/govtech/covid19/firebase/FCMInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,46 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.IOException;

@Service
@Slf4j
public class FCMInitializer {

@Value("${firebase.config-path}")
private String firebaseConfigPath;

Logger logger = LoggerFactory.getLogger(FCMInitializer.class);

@PostConstruct
public void initialize() {
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(new ClassPathResource(firebaseConfigPath).getInputStream())).build();
logger.info(options.toString());
FirebaseOptions options;
if (ResourceUtils.getFile(firebaseConfigPath).exists()) {
log.info("FCM credential loaded from filepath");
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(new FileInputStream(ResourceUtils.getFile(firebaseConfigPath)))).build();

} else {
log.info("FCM credential loaded from classpath");
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(new ClassPathResource(firebaseConfigPath).getInputStream())).build();

}

log.info(options.toString());
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
logger.info("Firebase application has been initialized");
log.info("Firebase application has been initialized");
}
} catch (IOException e) {
logger.error(e.getMessage());
log.error(e.getMessage());
}
}

Expand Down

0 comments on commit 7f85413

Please sign in to comment.