-
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/#52 fcm token #75
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6bf9938
[FEAT] FCM Token VO κΈ°λ₯ ꡬν
tidavid1 c2b77fb
Merge branch 'develop' into feat/#52-fcm_token
tidavid1 294297e
[FEAT] FCM Token Repository ꡬν
tidavid1 ff8966d
[FEAT] FCM Token μ μ₯ κΈ°λ₯ ꡬν
tidavid1 87ef422
[FEAT] FCM Token μ‘°ν λ‘μ§ μΆκ°
tidavid1 7595710
[FEAT] FCM Notification Config λ±λ‘
tidavid1 da5a6ac
[FEAT] FCM Notification μ μ‘ κΈ°λ₯ μΆκ°
tidavid1 3bddd1a
[FEAT] Api μ€λͺ
λ¬Έμ μΆκ°
tidavid1 f74156c
[FEAT] Swagger Schema μΆκ° λ° ErrorExamples μμΉ μμ
tidavid1 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
32 changes: 32 additions & 0 deletions
32
src/main/java/io/oeid/mogakgo/common/swagger/template/NotificationSwagger.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,32 @@ | ||
package io.oeid.mogakgo.common.swagger.template; | ||
|
||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
import io.oeid.mogakgo.core.properties.swagger.error.SwaggerProjectErrorExamples; | ||
import io.oeid.mogakgo.domain.notification.presentation.dto.req.FCMTokenApiRequest; | ||
import io.oeid.mogakgo.exception.dto.ErrorResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.ExampleObject; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@Tag(name = "Notification", description = "μλ¦Ό κ΄λ ¨ API") | ||
@SuppressWarnings("unused") | ||
public interface NotificationSwagger { | ||
|
||
@Operation(summary = "FCM ν ν° μ μ₯", description = "νμμ FCM ν ν°μ μ μ₯ν λ μ¬μ©νλ API") | ||
@ApiResponse(responseCode = "200", description = "FCM ν ν° μ μ₯ μ±κ³΅") | ||
@ApiResponse(responseCode = "404", description = "μμ²ν μ μ κ° μ‘΄μ¬νμ§ μμ", content = @Content( | ||
mediaType = APPLICATION_JSON_VALUE, | ||
schema = @Schema(implementation = ErrorResponse.class), | ||
examples = { | ||
@ExampleObject(name = "E020301", value = SwaggerProjectErrorExamples.USER_NOT_FOUND) | ||
}) | ||
) | ||
ResponseEntity<Void> manageFCMToken(@Parameter(hidden = true) Long userId, | ||
FCMTokenApiRequest request); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/io/oeid/mogakgo/core/configuration/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,43 @@ | ||
package io.oeid.mogakgo.core.configuration; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FCMConfig { | ||
|
||
private FirebaseApp firebaseApp; | ||
|
||
public FCMConfig(@Value("${firebase.key-path}") String keyPath) throws IOException { | ||
try (InputStream credentials = getClass().getClassLoader().getResourceAsStream(keyPath)) { | ||
List<FirebaseApp> firebaseApps = FirebaseApp.getApps(); | ||
if (firebaseApps != null && !firebaseApps.isEmpty()) { | ||
for (FirebaseApp app : firebaseApps) { | ||
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { | ||
firebaseApp = app; | ||
} | ||
} | ||
} else { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream( | ||
Objects.requireNonNull(credentials))) | ||
.build(); | ||
firebaseApp = FirebaseApp.initializeApp(options); | ||
} | ||
} | ||
} | ||
|
||
@Bean | ||
public FirebaseMessaging firebaseMessaging() { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ public class SwaggerProjectErrorExamples { | |
public static final String PROJECT_NOT_FOUND = "{\"timestamp\":\"2024-02-17T10:07:31.404Z\",\"statusCode\":404,\"code\":\"E030301\",\"message\":\"ν΄λΉ νλ‘μ νΈκ° μ‘΄μ¬νμ§ μμ΅λλ€.\"}"; | ||
public static final String PROJECT_FORBIDDEN_OPERATION = "{\"timestamp\":\"2024-02-17T10:07:31.404Z\",\"statusCode\":403,\"code\":\"E030201\",\"message\":\"ν΄λΉ νλ‘μ νΈμ λν κΆνμ΄ μμ΅λλ€.\"}"; | ||
public static final String PROJECT_DELETION_NOT_ALLOWED = "{\"timestamp\":\"2024-02-17T10:07:31.404Z\",\"statusCode\":400,\"code\":\"E030106\",\"message\":\"λ§€μΉ μ€μ΄κ±°λ λκΈ°μ€μΈ νλ‘μ νΈλ μμ ν μ μμ΅λλ€.\"}"; | ||
public static final String USER_NOT_FOUND = "{\"timestamp\":\"2024-02-17T10:07:31.404Z\",\"statusCode\":404,\"code\":\"E020301\",\"message\":\"ν΄λΉ μ μ κ° μ‘΄μ¬νμ§ μμ΅λλ€.\"}"; | ||
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. μ΄κ±° μ ν΄λΉ νμΌμ μμ£ ?? |
||
private SwaggerProjectErrorExamples() { | ||
} | ||
|
||
|
62 changes: 62 additions & 0 deletions
62
src/main/java/io/oeid/mogakgo/domain/notification/application/FCMNotificationService.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,62 @@ | ||
package io.oeid.mogakgo.domain.notification.application; | ||
|
||
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 io.oeid.mogakgo.domain.notification.domain.vo.FCMToken; | ||
import io.oeid.mogakgo.domain.notification.exception.NotificationException; | ||
import io.oeid.mogakgo.domain.notification.infrastructure.FCMTokenJpaRepository; | ||
import io.oeid.mogakgo.domain.user.application.UserCommonService; | ||
import io.oeid.mogakgo.exception.code.ErrorCode404; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class FCMNotificationService { | ||
|
||
private final FCMTokenJpaRepository fcmTokenRepository; | ||
private final UserCommonService userCommonService; | ||
private final FirebaseMessaging firebaseMessaging; | ||
|
||
@Transactional | ||
public void manageToken(Long userId, String fcmToken) { | ||
log.info("manageToken Start"); | ||
FCMToken token = fcmTokenRepository.findById(userCommonService.getUserById(userId).getId()) | ||
.orElseGet(() -> new FCMToken(userId, fcmToken)); | ||
token.updateToken(fcmToken); | ||
fcmTokenRepository.save(token); | ||
log.info("manageToken End"); | ||
} | ||
|
||
public void sendNotification(Long userId, String title, String body) { | ||
log.info("sendNotification Start"); | ||
String fcmToken = getFCMToken(userId); | ||
// send notification | ||
Message message = Message.builder() | ||
.setNotification(Notification.builder() | ||
.setTitle(title) | ||
.setBody(body) | ||
.build()) | ||
.setToken(fcmToken) | ||
.build(); | ||
try { | ||
String response = firebaseMessaging.send(message); | ||
log.info("Successfully sent message: " + response); | ||
} catch (FirebaseMessagingException e) { | ||
log.error("Error sending message: " + e.getMessage()); | ||
} | ||
log.info("sendNotification End"); | ||
} | ||
|
||
private String getFCMToken(Long userId) { | ||
return fcmTokenRepository.findById(userId) | ||
.map(FCMToken::getToken) | ||
.orElseThrow( | ||
() -> new NotificationException(ErrorCode404.NOTIFICATION_FCM_TOKEN_NOT_FOUND)); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/io/oeid/mogakgo/domain/notification/domain/vo/FCMToken.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,48 @@ | ||
package io.oeid.mogakgo.domain.notification.domain.vo; | ||
|
||
import io.oeid.mogakgo.domain.notification.exception.NotificationException; | ||
import io.oeid.mogakgo.exception.code.ErrorCode400; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Table(name = "fcm_token_tb") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
public class FCMToken { | ||
|
||
@Id | ||
@Column(name = "id") | ||
private Long userId; | ||
|
||
@Column(name = "token") | ||
private String token; | ||
|
||
public FCMToken(Long userId, String token) { | ||
this.userId = verifyUserId(userId); | ||
this.token = verifyToken(token); | ||
} | ||
|
||
public Long verifyUserId(Long userId) { | ||
if (userId == null) { | ||
throw new NotificationException(ErrorCode400.USER_ID_NOT_NULL); | ||
} | ||
return userId; | ||
} | ||
|
||
public String verifyToken(String fcmToken) { | ||
if (fcmToken == null || fcmToken.isBlank()) { | ||
throw new NotificationException(ErrorCode400.NOTIFICATION_FCM_TOKEN_NOT_NULL); | ||
} | ||
return fcmToken; | ||
} | ||
|
||
public void updateToken(String token) { | ||
this.token = verifyToken(token); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/io/oeid/mogakgo/domain/notification/infrastructure/FCMTokenJpaRepository.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,10 @@ | ||
package io.oeid.mogakgo.domain.notification.infrastructure; | ||
|
||
import io.oeid.mogakgo.domain.notification.domain.vo.FCMToken; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface FCMTokenJpaRepository extends JpaRepository<FCMToken, Long> { | ||
|
||
} |
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
30 changes: 30 additions & 0 deletions
30
src/main/java/io/oeid/mogakgo/domain/notification/presentation/NotificationController.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,30 @@ | ||
package io.oeid.mogakgo.domain.notification.presentation; | ||
|
||
import io.oeid.mogakgo.common.annotation.UserId; | ||
import io.oeid.mogakgo.common.swagger.template.NotificationSwagger; | ||
import io.oeid.mogakgo.domain.notification.application.FCMNotificationService; | ||
import io.oeid.mogakgo.domain.notification.presentation.dto.req.FCMTokenApiRequest; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/notification") | ||
@RequiredArgsConstructor | ||
public class NotificationController implements NotificationSwagger { | ||
|
||
private final FCMNotificationService fcmNotificationService; | ||
|
||
@PostMapping("/fcm") | ||
public ResponseEntity<Void> manageFCMToken(@UserId Long userId, @RequestBody @Valid | ||
FCMTokenApiRequest request) { | ||
fcmNotificationService.manageToken(userId, request.getFcmToken()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...ain/java/io/oeid/mogakgo/domain/notification/presentation/dto/req/FCMTokenApiRequest.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,14 @@ | ||
package io.oeid.mogakgo.domain.notification.presentation.dto.req; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class FCMTokenApiRequest { | ||
|
||
@NotBlank(message = "FCM ν ν°μ νμμ λλ€.") | ||
private final String fcmToken; | ||
} |
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
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.
μ¬κΈ°μ @parameter μλ¬μλ λλμ?