Skip to content

Commit

Permalink
[hotfix] fix: fcm init ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
sangcci committed Feb 17, 2025
1 parent fd9f884 commit e350708
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;

@Component
public class FcmInitializer {

@PostConstruct
public void initizlize() {
try (FileInputStream serviceAccount = new FileInputStream("./src/main/resources/firebase/firebase-service-account.json")) {
public void initialize() {
try (InputStream serviceAccount = getClass().getResourceAsStream("/firebase/firebase-service-account.json")) {
if (serviceAccount == null) {
throw new IOException("Firebase service account file not found in classpath!");
}
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
FirebaseApp.initializeApp(options);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Failed to initialize Firebase", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.List;
import lombok.NoArgsConstructor;
Expand All @@ -18,12 +18,14 @@ public final class FcmTokenUtils {
private static AccessToken accessToken;

static {
try (FileInputStream serviceAccount = new FileInputStream(KEY_PATH)) {
googleCredentials = GoogleCredentials
.fromStream(serviceAccount)
try (InputStream serviceAccount = FcmTokenUtils.class.getResourceAsStream(KEY_PATH)) {
if (serviceAccount == null) {
throw new IOException("Google service account JSON file not found in classpath: " + KEY_PATH);
}
googleCredentials = GoogleCredentials.fromStream(serviceAccount)
.createScoped(List.of("https://www.googleapis.com/auth/firebase.messaging"));
} catch (IOException e) {
log.error("GoogleCredentials ์ดˆ๊ธฐํ™” ์‹คํŒจ: {0}", e);
log.error("GoogleCredentials ์ดˆ๊ธฐํ™” ์‹คํŒจ");
throw new RuntimeException(e);
}
}
Expand All @@ -41,7 +43,7 @@ public static String getAccessToken() {
log.info("์ƒˆ ์—‘์„ธ์Šค ํ† ํฐ ๋ฐœ๊ธ‰. ๋งŒ๋ฃŒ ์‹œ๊ฐ„: {}", accessToken.getExpirationTime());
return accessToken.getTokenValue();
} catch (IOException e) {
log.error("์—‘์„ธ์Šค ํ† ํฐ ๋ฐœ๊ธ‰ ์‹คํŒจ: {0}", e);
log.error("์—‘์„ธ์Šค ํ† ํฐ ๋ฐœ๊ธ‰ ์‹คํŒจ");
throw new RuntimeException(e);
}
}
Expand Down

0 comments on commit e350708

Please sign in to comment.