-
Notifications
You must be signed in to change notification settings - Fork 3
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 #82 from HongDam-org/feat/apn
[FEAT] Firebase 연동 및 FCM Service 기본 Logic 구현
- Loading branch information
Showing
8 changed files
with
102 additions
and
4 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
34 changes: 34 additions & 0 deletions
34
backend/src/main/java/com/twtw/backend/config/firebase/FcmConfig.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,34 @@ | ||
package com.twtw.backend.config.firebase; | ||
|
||
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 FcmConfig { | ||
private static final ClassPathResource FIREBASE_RESOURCE = | ||
new ClassPathResource("backend/src/main/resources/firebase/twtw_firebase_key.json"); | ||
|
||
@Bean | ||
public FirebaseApp firebaseApp() throws IOException { | ||
FirebaseOptions options = | ||
FirebaseOptions.builder() | ||
.setCredentials( | ||
GoogleCredentials.fromStream(FIREBASE_RESOURCE.getInputStream())) | ||
.build(); | ||
|
||
return FirebaseApp.initializeApp(options); | ||
} | ||
|
||
@Bean | ||
public 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
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/twtw/backend/domain/notification/dto/NotificationRequest.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.twtw.backend.domain.notification.dto; | ||
|
||
import com.google.firebase.messaging.Notification; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class NotificationRequest { | ||
@NotBlank private String deviceToken; | ||
|
||
private String title; | ||
|
||
private String body; | ||
|
||
public Notification toNotification() { | ||
return new Notification(title, body); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/com/twtw/backend/domain/notification/mapper/NotificationMapper.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,6 @@ | ||
package com.twtw.backend.domain.notification.mapper; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class NotificationMapper {} |
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/com/twtw/backend/domain/notification/service/FcmService.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,27 @@ | ||
package com.twtw.backend.domain.notification.service; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import com.twtw.backend.domain.notification.dto.NotificationRequest; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class FcmService { | ||
private final FirebaseMessaging firebaseMessaging; | ||
|
||
public FcmService(FirebaseMessaging firebaseMessaging) { | ||
this.firebaseMessaging = firebaseMessaging; | ||
} | ||
|
||
public void sendNotification(NotificationRequest request) throws FirebaseMessagingException { | ||
Message message = | ||
Message.builder() | ||
.setToken(request.getDeviceToken()) | ||
.setNotification(request.toNotification()) | ||
.build(); | ||
|
||
firebaseMessaging.send(message); | ||
} | ||
} |
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