-
Notifications
You must be signed in to change notification settings - Fork 1
feat: fcm V2추가 #80
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: fcm V2추가 #80
Changes from all 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 |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ | |
| import com.google.firebase.messaging.ApnsConfig; | ||
| import com.google.firebase.messaging.Aps; | ||
| import com.google.firebase.messaging.BatchResponse; | ||
| import com.google.firebase.messaging.FirebaseMessaging; | ||
| import com.google.firebase.messaging.FirebaseMessagingException; | ||
| import com.google.firebase.messaging.MessagingErrorCode; | ||
| import com.google.firebase.messaging.MulticastMessage; | ||
|
|
@@ -31,6 +30,7 @@ public class FCMService { | |
|
|
||
| private final FCMRepository fcmRepository; | ||
| private final HeaderAuthValidator headerAuthValidator; | ||
| private final FirebaseMessagingClient firebaseMessagingClient; | ||
|
|
||
| /** | ||
| * (핵심 메소드) 특정 User에게 알림을 발송합니다. | ||
|
|
@@ -65,7 +65,7 @@ public void sendNotificationToUser(User user, String title, String body) { | |
| // FCM에 일괄 발송 요청 | ||
| BatchResponse response; | ||
| try { | ||
| response = FirebaseMessaging.getInstance().sendEachForMulticast(message); | ||
| response = firebaseMessagingClient.sendEachForMulticast(message); | ||
|
|
||
| log.info(EXTERNAL_API, "FCM 알림 발송 완료 {} {} {}", | ||
| kv("total", response.getSuccessCount() + response.getFailureCount()), | ||
|
|
@@ -135,13 +135,40 @@ private static String maskToken(String token) { | |
| } | ||
|
|
||
| @Transactional | ||
| public void updateFcmToken(String userUuid, String deviceSecret, String SFcmToken) { | ||
| public void updateFcmToken(String userUuid, String deviceSecret, String requestToken) { | ||
|
|
||
| User user = headerAuthValidator.validateAndGetUser(userUuid, deviceSecret); | ||
|
|
||
| FCM_Token fcmToken = fcmRepository.findByUserId(user.getId()); | ||
| List<FCM_Token> FCM_Tokens = fcmRepository.findByUser(user); | ||
|
|
||
| fcmToken.update(SFcmToken); | ||
| if(FCM_Tokens.isEmpty()){ | ||
| FCM_Token newToken = FCM_Token.create(user, requestToken, "unknown"); | ||
| fcmRepository.save(newToken); | ||
| } else { | ||
| FCM_Token existingToken = FCM_Tokens.get(0); | ||
| if (!existingToken.getFcmToken().equals(requestToken)) { | ||
| existingToken.updateToken(requestToken); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Transactional | ||
| public void updateFcmTokenV2(String userUuid, String deviceSecret, String requestToken, String model) { | ||
|
|
||
| User user = headerAuthValidator.validateAndGetUser(userUuid, deviceSecret); | ||
|
|
||
| List<FCM_Token> fcmTokens = fcmRepository.findByUser(user); | ||
|
|
||
| if (fcmTokens.isEmpty()) { | ||
| FCM_Token newToken = FCM_Token.create(user, requestToken, model); | ||
| fcmRepository.save(newToken); | ||
| } else { | ||
| FCM_Token existingToken = fcmTokens.get(0); | ||
| if (!existingToken.getFcmToken().equals(requestToken)) { | ||
| existingToken.updateToken(requestToken); | ||
| } | ||
| } | ||
|
Comment on lines
+161
to
+171
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. 현재 또한, 기존 토큰이 존재할 경우 토큰 값만 업데이트하고 이 로직은 사용자가 단 하나의 기기만 사용한다고 가정하는 것으로 보입니다. 여러 기기 지원이 필요하다면 아래와 같은 방식으로 재설계하는 것을 고려해 보세요.
|
||
| } | ||
|
Comment on lines
137
to
172
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.
예시: @Transactional
public void updateFcmToken(String userUuid, String deviceSecret, String requestToken) {
User user = headerAuthValidator.validateAndGetUser(userUuid, deviceSecret);
upsertFcmToken(user, requestToken, "unknown");
}
@Transactional
public void updateFcmTokenV2(String userUuid, String deviceSecret, String requestToken, String model) {
User user = headerAuthValidator.validateAndGetUser(userUuid, deviceSecret);
upsertFcmToken(user, requestToken, model);
}
private void upsertFcmToken(User user, String requestToken, String model) {
// 현재 updateFcmTokenV2의 로직을 여기에 구현
List<FCM_Token> fcmTokens = fcmRepository.findByUser(user);
if (fcmTokens.isEmpty()) {
FCM_Token newToken = FCM_Token.create(user, requestToken, model);
fcmRepository.save(newToken);
} else {
FCM_Token existingToken = fcmTokens.get(0);
if (!existingToken.getFcmToken().equals(requestToken)) {
existingToken.updateToken(requestToken);
}
}
} |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.todaysound.todaysound_server.global.application; | ||
|
|
||
| import com.google.firebase.messaging.BatchResponse; | ||
| import com.google.firebase.messaging.FirebaseMessaging; | ||
| import com.google.firebase.messaging.FirebaseMessagingException; | ||
| import com.google.firebase.messaging.MulticastMessage; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class FirebaseMessagingClient { | ||
|
|
||
| public BatchResponse sendEachForMulticast(MulticastMessage message) throws FirebaseMessagingException { | ||
| return FirebaseMessaging.getInstance().sendEachForMulticast(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.todaysound.todaysound_server.global.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public record FCMUpdateRequestV2( | ||
| @NotBlank String fcmToken, | ||
| @NotBlank String model | ||
| ) { | ||
| } |
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.
updateFcmToken메소드에 몇 가지 개선점이 있습니다.[Critical] 149행의
!existingToken.equals(requestToken)비교 로직에 버그가 있습니다.existingToken은FCM_Token객체이고requestToken은String이므로,Object.equals()는 항상false를 반환합니다. 따라서 토큰이 갱신되어야 할 때 갱신되지 않습니다.existingToken.getFcmToken().equals(requestToken)으로 토큰 문자열 값을 직접 비교해야 합니다.[Medium] 142행의
FCM_Tokens변수명은 Java 네이밍 컨벤션(camelCase)에 따라fcmTokens로 수정하는 것이 좋습니다.