diff --git a/core/network/src/main/java/com/easyhz/noffice/core/network/api/auth/AuthService.kt b/core/network/src/main/java/com/easyhz/noffice/core/network/api/auth/AuthService.kt index 8f0b17fc..ddb612c4 100644 --- a/core/network/src/main/java/com/easyhz/noffice/core/network/api/auth/AuthService.kt +++ b/core/network/src/main/java/com/easyhz/noffice/core/network/api/auth/AuthService.kt @@ -19,4 +19,10 @@ interface AuthService { /* 단일 회원 정보 조회 */ @GET("/api/v1/member") suspend fun fetchUserInfo(): NofficeResult + + /* fcm 토큰 저장 */ + @POST("/api/v1/notifications/fcm-token") + suspend fun registerMessagingToken( + @Body fcmToken: String + ): NofficeResult } \ No newline at end of file diff --git a/data/notification/build.gradle.kts b/data/notification/build.gradle.kts index 17bf2343..4d7a22a1 100644 --- a/data/notification/build.gradle.kts +++ b/data/notification/build.gradle.kts @@ -11,4 +11,5 @@ android { dependencies { implementation(projects.core.common) + implementation(projects.core.network) } \ No newline at end of file diff --git a/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepository.kt b/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepository.kt index 84207f4a..1971d160 100644 --- a/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepository.kt +++ b/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepository.kt @@ -2,4 +2,5 @@ package com.easyhz.noffice.data.notification.repository.messaging interface CloudMessagingRepository { suspend fun getToken(): Result + suspend fun registerToken(token: String): Result } \ No newline at end of file diff --git a/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepositoryImpl.kt b/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepositoryImpl.kt index dade0d32..63c4a548 100644 --- a/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepositoryImpl.kt +++ b/data/notification/src/main/java/com/easyhz/noffice/data/notification/repository/messaging/CloudMessagingRepositoryImpl.kt @@ -2,6 +2,8 @@ package com.easyhz.noffice.data.notification.repository.messaging import com.easyhz.noffice.core.common.di.Dispatcher import com.easyhz.noffice.core.common.di.NofficeDispatchers +import com.easyhz.noffice.core.network.api.auth.AuthService +import com.easyhz.noffice.core.network.util.toResult import com.google.firebase.messaging.FirebaseMessaging import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.tasks.await @@ -10,11 +12,16 @@ import javax.inject.Inject class CloudMessagingRepositoryImpl @Inject constructor( @Dispatcher(NofficeDispatchers.IO) private val dispatcher: CoroutineDispatcher, - private val messagingInstance: FirebaseMessaging + private val messagingInstance: FirebaseMessaging, + private val authService: AuthService ): CloudMessagingRepository { override suspend fun getToken(): Result = withContext(dispatcher) { runCatching { messagingInstance.token.await() } } + + override suspend fun registerToken(token: String): Result = withContext(dispatcher) { + return@withContext authService.registerMessagingToken(token).toResult() + } } \ No newline at end of file diff --git a/data/notification/src/main/java/com/easyhz/noffice/data/notification/service/CloudMessagingService.kt b/data/notification/src/main/java/com/easyhz/noffice/data/notification/service/CloudMessagingService.kt index e0e9fc0a..96870860 100644 --- a/data/notification/src/main/java/com/easyhz/noffice/data/notification/service/CloudMessagingService.kt +++ b/data/notification/src/main/java/com/easyhz/noffice/data/notification/service/CloudMessagingService.kt @@ -1,13 +1,23 @@ package com.easyhz.noffice.data.notification.service import android.util.Log +import com.easyhz.noffice.core.network.api.auth.AuthService import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import javax.inject.Inject +@AndroidEntryPoint class CloudMessagingService: FirebaseMessagingService() { + @Inject + lateinit var authService: AuthService override fun onNewToken(token: String) { super.onNewToken(token) Log.d("FCM SERVICE ", "Refreshed token: $token") + registerToken(token) } override fun onMessageReceived(message: RemoteMessage) { @@ -17,4 +27,15 @@ class CloudMessagingService: FirebaseMessagingService() { service.showNotification(message) } + private fun registerToken(token: String) { + CoroutineScope(Dispatchers.IO).launch { + val response = authService.registerMessagingToken(token) + response.onSuccess { + Log.d(this.javaClass.name, "Success registering token") + }.onFailure { e -> + Log.e(this.javaClass.name, "Error registering token", e) + } + } + } + } \ No newline at end of file diff --git a/domain/notification/src/main/java/com/easyhz/noffice/domain/notification/usecase/RegisterMessagingTokenUseCase.kt b/domain/notification/src/main/java/com/easyhz/noffice/domain/notification/usecase/RegisterMessagingTokenUseCase.kt new file mode 100644 index 00000000..380e0a9b --- /dev/null +++ b/domain/notification/src/main/java/com/easyhz/noffice/domain/notification/usecase/RegisterMessagingTokenUseCase.kt @@ -0,0 +1,22 @@ +package com.easyhz.noffice.domain.notification.usecase + +import com.easyhz.noffice.core.common.base.BaseUseCase +import com.easyhz.noffice.data.notification.repository.messaging.CloudMessagingRepository +import javax.inject.Inject + +class RegisterMessagingTokenUseCase @Inject constructor( + private val cloudMessagingRepository: CloudMessagingRepository, +) : BaseUseCase() { + override suspend fun invoke(param: Unit): Result = runCatching { + val token = getMessagingToken().getOrThrow() + registerMessagingToken(token) + } + + private suspend fun getMessagingToken(): Result { + return cloudMessagingRepository.getToken() + } + + private suspend fun registerMessagingToken(token: String): Result { + return cloudMessagingRepository.registerToken(token) + } +} \ No newline at end of file diff --git a/domain/sign/build.gradle.kts b/domain/sign/build.gradle.kts index dd6b5d3a..a9da7559 100644 --- a/domain/sign/build.gradle.kts +++ b/domain/sign/build.gradle.kts @@ -12,6 +12,7 @@ dependencies { api(projects.core.model) implementation(projects.core.common) implementation(projects.data.auth) + implementation(projects.data.notification) implementation(libs.googleid) implementation(libs.gms.play.services.auth) } \ No newline at end of file diff --git a/feature/sign/build.gradle.kts b/feature/sign/build.gradle.kts index 7861c326..7dd045f4 100644 --- a/feature/sign/build.gradle.kts +++ b/feature/sign/build.gradle.kts @@ -13,4 +13,5 @@ dependencies { implementation(projects.core.designSystem) implementation(projects.core.common) implementation(projects.domain.sign) + implementation(projects.domain.notification) } \ No newline at end of file diff --git a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt index fda55f3e..ed4ca61f 100644 --- a/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt +++ b/feature/sign/src/main/java/com/easyhz/noffice/feature/sign/screen/login/LoginViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.viewModelScope import com.easyhz.noffice.core.common.base.BaseViewModel import com.easyhz.noffice.core.common.error.handleError import com.easyhz.noffice.core.model.auth.param.AuthParam +import com.easyhz.noffice.domain.notification.usecase.RegisterMessagingTokenUseCase import com.easyhz.noffice.domain.sign.usecase.LoginUseCase import com.easyhz.noffice.feature.sign.contract.login.LoginIntent import com.easyhz.noffice.feature.sign.contract.login.LoginSideEffect @@ -17,7 +18,8 @@ import javax.inject.Inject @HiltViewModel class LoginViewModel @Inject constructor( - private val loginUseCase: LoginUseCase + private val loginUseCase: LoginUseCase, + private val registerMessagingTokenUseCase: RegisterMessagingTokenUseCase, ): BaseViewModel( initialState = LoginState.init() ) { @@ -31,6 +33,7 @@ class LoginViewModel @Inject constructor( setIsLoading(true) loginUseCase.invoke(AuthParam(context, type.name)).onSuccess { // FIXME 가입 된 유저 네비게이션 처리 + registerMessagingTokenUseCase(Unit).getOrNull() navigateToHome() }.onFailure { it.printStackTrace()