I am student learning android application developing and now I want to develop application that I will make user online and offline I have API for both of them which I send user token to API.
I make API request that make user online successfully and it works well
but now I have a problem with API that make user offline.
I called it in workManager, broadcast receiver and in service but it doesn't work correctly when user remove app from recent apps
so I want an approach to make user offline when he closed the applications (with all cases).
my API request is
http://android.somee.com/api/User/SetDeviceOffline
and this request take Bearer token
I'm using MVVM with coroutines and this is my code to set device off
APIService.kt
@POST("User/SetDeviceOffline")
suspend fun setDeviceOff(@Header("Authorization") token: String): Response<Void>
Repo.kt
suspend fun setDeviceOff(token: String):Response<Void> =
withContext(Dispatchers.IO) {
apiService.setDeviceOff(token)
}
viewModel.kt
fun setDeviceOff(token: String) = viewModelScope.launch {
val result = authRepository.setDeviceOff(token)
_changeDeviceStatus.postValue(result.isSuccessful)
}
this is normal way but it doesn't work when application in background too
so what can I do to achieve my needs ?