-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f138aea
commit 7e5aa44
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
Infra/src/main/java/com/example/fcm/service/FcmInitializer.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.example.fcm.service; | ||
|
||
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.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
@Configuration | ||
public class FcmInitializer { | ||
@Value("${app.firebase-configuration-file}") | ||
private String firebaseConfigPath; | ||
|
||
@Bean | ||
FirebaseMessaging firebaseMessaging() throws IOException { | ||
ClassPathResource resource = new ClassPathResource(firebaseConfigPath); | ||
InputStream resourceInputStream = resource.getInputStream(); | ||
FirebaseApp firebaseApp = null; | ||
List<FirebaseApp> apps = FirebaseApp.getApps(); | ||
|
||
if (apps != null && !apps.isEmpty()) { | ||
for (FirebaseApp app : apps) { | ||
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { | ||
firebaseApp = app; | ||
} | ||
} | ||
} | ||
else { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(resourceInputStream)).build(); | ||
firebaseApp = FirebaseApp.initializeApp(options); | ||
} | ||
return FirebaseMessaging.getInstance(firebaseApp); | ||
} | ||
} |