Skip to content

Commit

Permalink
refactor: FCM 초기화 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim0914 committed Jan 22, 2024
1 parent 69dbe3a commit b92cfc6
Showing 1 changed file with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public void initialize() {
try {
serviceAccount = new FileInputStream(JSON_FILE_PATH);
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();

FirebaseApp.initializeApp(options);
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options, "Pium");
}
} catch (FileNotFoundException e) {
log.error("파일을 찾을 수 없습니다. " + e);
} catch (IOException e) {
Expand All @@ -57,13 +59,13 @@ public void initialize() {

public void sendMessageTo(String targetToken, String title, String body) {
Notification notification = Notification.builder()
.setTitle(title)
.setBody(body)
.build();
.setTitle(title)
.setBody(body)
.build();
Message message = Message.builder()
.setToken(targetToken)
.setNotification(notification)
.build();
.setToken(targetToken)
.setNotification(notification)
.build();
try {
String response = FirebaseMessaging.getInstance().sendAsync(message).get();
log.info("응답 결과 : " + response);
Expand Down Expand Up @@ -100,24 +102,24 @@ public void sendMessageTo(String targetToken, String title, String body) {

private FcmMessageResponse makeMessage(String targetToken, String title, String body) {
return FcmMessageResponse.builder()
.message(FcmMessageResponse.Message.builder()
.token(targetToken)
.notification(FcmMessageResponse.Notification.builder()
.title(title)
.body(body)
.image(null)
.build()
.message(FcmMessageResponse.Message.builder()
.token(targetToken)
.notification(FcmMessageResponse.Notification.builder()
.title(title)
.body(body)
.image(null)
.build()
)
.build()
)
.build()
)
.validate_only(false)
.build();
.validate_only(false)
.build();
}

private String getAccessToken() throws IOException {
GoogleCredentials googleCredentials = GoogleCredentials
.fromStream(new ClassPathResource(keyPath).getInputStream())
.createScoped(keyScope);
.fromStream(new ClassPathResource(keyPath).getInputStream())
.createScoped(keyScope);
googleCredentials.refreshIfExpired();
return googleCredentials.getAccessToken().getTokenValue();
}
Expand Down

0 comments on commit b92cfc6

Please sign in to comment.