Created
November 16, 2018 10:19
-
-
Save nvtuan305/9c6f1f5cec1d3aa4df27f6f6c51ce396 to your computer and use it in GitHub Desktop.
Firebase Admin SDK configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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