-
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,4 @@ out/ | |
.vscode/ | ||
|
||
application.yml | ||
firebase_key.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.fullcar.carpool.application.Alarm; | ||
|
||
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 AlarmService { | ||
|
||
private final FirebaseMessaging firebaseMessaging; | ||
|
||
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. ToDoλ‘ μ΄μ νλμκ²μ! |
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()); | ||
} | ||
} |
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.
μ λ² μ΄λ©μΌ μΈμ¦κ³Ό λμΌνκ² νΈμμλ¦Ό λν μΈλΆμμ€ν μ ν΅ν΄ 보λ΄λ κ²μ΄κΈ° λλ¬Έμ λλ©μΈμλΉμ€μ μΈν°νμ΄μ€λ₯Ό μ μνκ³ infra μμμμ PushMessageClientκ³Ό κ°μ κ°μ²΄λ₯Ό λ§λ€μ΄ sendNotification() ν¨μλ₯Ό ꡬννλ κ²λ μ’μ κ² κ°μ΅λλ€!
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.
λ°μμλ£νμ΅λλ€!