Skip to content

Commit

Permalink
Merge pull request #119 from Team-Notitime/feat/fcm/118-fcm-api-conne…
Browse files Browse the repository at this point in the history
…ction

Feat/fcm/118 fcm api connection
  • Loading branch information
easyhz authored Aug 21, 2024
2 parents affc7fa + 55bb094 commit f4adf83
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ interface AuthService {
/* 단일 회원 정보 조회 */
@GET("/api/v1/member")
suspend fun fetchUserInfo(): NofficeResult<UserInfoResponse>

/* fcm 토큰 저장 */
@POST("/api/v1/notifications/fcm-token")
suspend fun registerMessagingToken(
@Body fcmToken: String
): NofficeResult<Unit>
}
1 change: 1 addition & 0 deletions data/notification/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ android {

dependencies {
implementation(projects.core.common)
implementation(projects.core.network)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.easyhz.noffice.data.notification.repository.messaging

interface CloudMessagingRepository {
suspend fun getToken(): Result<String>
suspend fun registerToken(token: String): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> = withContext(dispatcher) {
runCatching {
messagingInstance.token.await()
}
}

override suspend fun registerToken(token: String): Result<Unit> = withContext(dispatcher) {
return@withContext authService.registerMessagingToken(token).toResult()
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Unit, Unit>() {
override suspend fun invoke(param: Unit): Result<Unit> = runCatching {
val token = getMessagingToken().getOrThrow()
registerMessagingToken(token)
}

private suspend fun getMessagingToken(): Result<String> {
return cloudMessagingRepository.getToken()
}

private suspend fun registerMessagingToken(token: String): Result<Unit> {
return cloudMessagingRepository.registerToken(token)
}
}
1 change: 1 addition & 0 deletions domain/sign/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions feature/sign/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies {
implementation(projects.core.designSystem)
implementation(projects.core.common)
implementation(projects.domain.sign)
implementation(projects.domain.notification)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<LoginState, LoginIntent, LoginSideEffect>(
initialState = LoginState.init()
) {
Expand All @@ -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()
Expand Down

0 comments on commit f4adf83

Please sign in to comment.