-
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.
Merge pull request #131 from 9uttery/feat/firebase-integration-#130
[Configuration] Firebase 기본 설정
- Loading branch information
Showing
7 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/guttery/madii/common/config/firebase/FirebaseConfig.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,40 @@ | ||
package com.guttery.madii.common.config.firebase; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.guttery.madii.common.exception.CustomException; | ||
import com.guttery.madii.common.exception.ErrorDetails; | ||
import jakarta.annotation.PostConstruct; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
|
||
@Configuration | ||
public class FirebaseConfig { | ||
@Value("${firebase.config}") | ||
private String firebaseKeyFilePath; | ||
@Value("${firebase.project-id}") | ||
private String firebaseProjectId; | ||
|
||
@PostConstruct | ||
public void initialize() { | ||
try ( | ||
final InputStream serviceAccount = new FileInputStream(firebaseKeyFilePath) | ||
) { | ||
final FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.setProjectId(firebaseProjectId) | ||
.build(); | ||
|
||
|
||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
} | ||
} catch (final Exception e) { | ||
throw CustomException.of(ErrorDetails.FIREBASE_INTEGRATION_FAILED); | ||
} | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
src/main/java/com/guttery/madii/domain/notification/domain/service/NotificationClient.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,4 @@ | ||
package com.guttery.madii.domain.notification.domain.service; | ||
|
||
public interface NotificationClient { | ||
} |
24 changes: 24 additions & 0 deletions
24
...java/com/guttery/madii/domain/notification/infrastructure/FirebaseNotificationClient.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,24 @@ | ||
package com.guttery.madii.domain.notification.infrastructure; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import com.guttery.madii.domain.notification.domain.service.NotificationClient; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class FirebaseNotificationClient implements NotificationClient { | ||
public String sendNotification(String content, String token) { | ||
try { | ||
final Message message = Message.builder() | ||
.setToken(token) | ||
.putData("title", content) | ||
.build(); | ||
|
||
return FirebaseMessaging.getInstance().send(message); | ||
|
||
} catch (FirebaseMessagingException e) { | ||
return "Failed"; | ||
} | ||
} | ||
} |