-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: push notification #73
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,4 @@ out/ | |
.vscode/ | ||
|
||
application.yml | ||
firebase_key.json |
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/fullcar/carpool/domain/service/NotificationService.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,8 @@ | ||
package com.fullcar.carpool.domain.service; | ||
|
||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional | ||
public interface NotificationService { | ||
void sendNotification(String nickname, String deviceToken, String title, String body); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/fullcar/carpool/infra/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,39 @@ | ||
package com.fullcar.carpool.infra; | ||
|
||
import com.fullcar.carpool.domain.service.NotificationService; | ||
import com.fullcar.core.exception.CustomException; | ||
import com.fullcar.core.response.ErrorCode; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class NotificationClient implements NotificationService { | ||
private final FirebaseMessaging firebaseMessaging; | ||
|
||
@Override | ||
public void sendNotification(String nickname, String deviceToken, String title, String body) { | ||
Notification notification = Notification.builder() | ||
.setTitle(nickname + "λ! " + title) | ||
.setBody(body) | ||
.build(); | ||
|
||
Message message = Message.builder() | ||
.setToken(deviceToken) | ||
.setNotification(notification) | ||
.build(); | ||
|
||
try { | ||
firebaseMessaging.send(message); | ||
} | ||
catch (FirebaseMessagingException e){ | ||
System.out.println(e); | ||
throw new CustomException(ErrorCode.FAILED_TO_SEND_NOTIFICATION); | ||
} | ||
} | ||
} |
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,37 @@ | ||
package com.fullcar.core.config; | ||
|
||
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 org.springframework.core.io.ClassPathResource; | ||
|
||
import java.io.IOException; | ||
|
||
@Configuration | ||
public class FirebaseConfig { | ||
|
||
private final ClassPathResource firebaseResource = new ClassPathResource("firebase/firebase_key.json"); | ||
|
||
@Bean | ||
FirebaseApp firebaseApp() throws IOException { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream( | ||
firebaseResource.getInputStream())) | ||
.build(); | ||
|
||
if (FirebaseApp.getApps().isEmpty()) { | ||
return FirebaseApp.initializeApp(options); | ||
} | ||
else { | ||
return FirebaseApp.getApps().get(0); | ||
} | ||
} | ||
|
||
@Bean | ||
FirebaseMessaging firebaseMessaging() throws IOException { | ||
return FirebaseMessaging.getInstance(firebaseApp()); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νΈμμλ¦Όμ μΈλΆμμ€ν μ ν΅ν΄ 보λ΄λ κ²μ΄κΈ° λλ¬Έμ FormServiceμ κ²°ν©λ ννλ‘ κ°λ°λλ€λ©΄ μΈλΆμμ€ν μ μμ‘΄νκ² λ κ°λ₯μ±μ΄ μλ€κ³ μκ°ν©λλ€! λ°λΌμ Spring Eventλ₯Ό νμ©νμ¬ κ²°ν©λλ₯Ό λμ¨νκ² νλ λ°©μλ μ’μ κ² κ°μ΅λλ€!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ToDoλ‘ μ΄μ νλμκ²μ!