-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TNT-107] feat: FCM 관련 세팅 및 어댑터 구현 (#19)
* [TNT-107] feat: FCM 공통 adapter 구현 * [TNT-107] feat: 로그인 시 FCM 토큰 확인 및 갱신 구현 * [TNT-107] test: 테스트 코드 작성 * [TNT-107] fix: sonar * [TNT-107] fix: sonar 문법 수정
- Loading branch information
Showing
12 changed files
with
147 additions
and
71 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.tnt.global.config; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
|
||
@Configuration | ||
@Profile({"prod", "dev"}) | ||
public class FcmConfig { | ||
|
||
@Value("${firebase.config.path}") | ||
private String firebaseConfigPath; | ||
|
||
@Bean | ||
public FirebaseApp firebaseApp() throws IOException { | ||
FileInputStream serviceAccount = new FileInputStream(firebaseConfigPath); | ||
|
||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.build(); | ||
|
||
return FirebaseApp.initializeApp(options); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.tnt.infrastructure.fcm; | ||
|
||
import static com.tnt.global.error.model.ErrorMessage.*; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import com.tnt.global.error.exception.TnTException; | ||
|
||
@Component | ||
public class FcmAdapter { | ||
|
||
public void sendNotificationByToken(String token, String title, String body, String clickActionUrl) { | ||
Message message = Message.builder() | ||
.setToken(token) | ||
.setNotification(com.google.firebase.messaging.Notification.builder() | ||
.setTitle(title) | ||
.setBody(body) | ||
.build()) | ||
.putData("click_action", clickActionUrl) | ||
.build(); | ||
|
||
try { | ||
FirebaseMessaging.getInstance().send(message); | ||
} catch (FirebaseMessagingException e) { | ||
throw new TnTException(FCM_FAILED, e); | ||
} | ||
} | ||
} |
Submodule config
updated
from 092fd8 to df369f
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package com.tnt.domain.member; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.within; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
|
@@ -102,5 +101,33 @@ void verify_tsid_timestamp_success() { | |
// then | ||
assertThat(timestamp).isCloseTo(Instant.now(), within(1, ChronoUnit.SECONDS)); | ||
} | ||
|
||
@Test | ||
@DisplayName("FCM 토큰 갱신 성공") | ||
void update_fcm_token_success() { | ||
// given | ||
Member member = Member.builder() | ||
.id(TSID.fast().toLong()) // TSID 직접 생성 | ||
.socialId("12345") | ||
.fcmToken("old-fcm-token") | ||
.email("[email protected]") | ||
.name("홍길동") | ||
.birthday(LocalDate.parse("2022-01-01")) | ||
.profileImageUrl("http://example.com") | ||
.serviceAgreement(true) | ||
.collectionAgreement(true) | ||
.advertisementAgreement(true) | ||
.pushAgreement(true) | ||
.socialType(SocialType.KAKAO) | ||
.build(); | ||
|
||
String newFcmToken = "new-fcm-token"; | ||
|
||
// when | ||
member.updateFcmTokenIfExpired(newFcmToken); | ||
|
||
// then | ||
assertThat(member.getFcmToken()).isEqualTo(newFcmToken); | ||
} | ||
} | ||
} |
Oops, something went wrong.