Skip to content

Instantly share code, notes, and snippets.

@nvtuan305
Created November 16, 2018 10:19
Show Gist options
  • Save nvtuan305/9c6f1f5cec1d3aa4df27f6f6c51ce396 to your computer and use it in GitHub Desktop.
Save nvtuan305/9c6f1f5cec1d3aa4df27f6f6c51ce396 to your computer and use it in GitHub Desktop.
Firebase Admin SDK configuration
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import java.io.InputStream;
/**
* @author nvtuan
* @since 11/16/18
*/
@Configuration
public class FirebaseConfig implements InitializingBean {
@Value("${fcm.serviceAccountPath}")
private String serviceAccountPath;
private ResourceLoader resourceLoader;
@Autowired
public FirebaseConfig(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Override
public void afterPropertiesSet() throws Exception {
initializeSDK();
}
private void initializeSDK() {
try {
InputStream inputStream = resourceLoader.getResource(serviceAccountPath).getInputStream();
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(inputStream))
.build();
FirebaseApp.initializeApp(options);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment