Skip to content

Commit

Permalink
Merge pull request #83 from Donut-DONationUTile/feature/fcm
Browse files Browse the repository at this point in the history
Feature/fcm
  • Loading branch information
Ganghee-Lee-0522 committed Apr 30, 2024
2 parents a580d26 + 008c03c commit 6586bd4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
FirebaseConfig.*
FcmUtils.*
FirebaseConfig.*
FcmController.*
FcmService.*

HELP.md
.gradle
build/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/zero/eight/donut/config/firebase/FcmUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.firebase.messaging.Notification;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import zero.eight.donut.config.jwt.AuthUtils;
import zero.eight.donut.domain.FcmToken;
import zero.eight.donut.domain.Giver;
Expand All @@ -21,6 +22,7 @@

@Slf4j
@RequiredArgsConstructor
@Component
public class FcmUtils {

private final AuthUtils authUtils;
Expand Down Expand Up @@ -90,4 +92,4 @@ public Message makeMessage(String targetToken, String title, String body) {
.setToken(targetToken)
.build();
}
}
}
25 changes: 23 additions & 2 deletions src/main/java/zero/eight/donut/config/firebase/FirebaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@Configuration
public class FirebaseConfig {
Expand All @@ -25,4 +26,24 @@ public void init() {
e.printStackTrace();
}
}
}


@Bean
public FirebaseMessaging firebaseMessaging() {
try {
FileInputStream serviceAccount =
new FileInputStream("src/main/resources/serviceAccountKey.json");

FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();

if (FirebaseApp.getApps().isEmpty()) { // FirebaseApp의 중복 초기화 방지.
FirebaseApp.initializeApp(options);
}
return FirebaseMessaging.getInstance();
} catch (Exception e) {
throw new IllegalStateException("Firebase Messaging 서비스를 초기화하는 동안 문제가 발생했습니다.", e);
}
}
}

0 comments on commit 6586bd4

Please sign in to comment.