mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 05:47:05 +09:00
DROID-3636 Notifications | Firebase messaging service implementation (#2383)
This commit is contained in:
parent
0e9a997998
commit
01638ff2f7
26 changed files with 273 additions and 10 deletions
|
@ -0,0 +1,54 @@
|
|||
package com.anytypeio.anytype.device
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import com.anytypeio.anytype.domain.base.AppCoroutineDispatchers
|
||||
import com.anytypeio.anytype.domain.base.fold
|
||||
import com.anytypeio.anytype.domain.device.DeviceTokenStoringService
|
||||
import com.anytypeio.anytype.domain.notifications.RegisterDeviceToken
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
class DeviceTokenStoringServiceImpl @Inject constructor(
|
||||
private val sharedPreferences: SharedPreferences,
|
||||
private val registerDeviceToken: RegisterDeviceToken,
|
||||
private val dispatchers: AppCoroutineDispatchers,
|
||||
private val scope: CoroutineScope
|
||||
) : DeviceTokenStoringService {
|
||||
|
||||
override fun saveToken(token: String) {
|
||||
scope.launch(dispatchers.io) {
|
||||
Timber.d("Saving token: $token")
|
||||
sharedPreferences.edit().apply {
|
||||
putString(PREF_KEY, token)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun start() {
|
||||
val token = sharedPreferences.getString(PREF_KEY, null)
|
||||
if (!token.isNullOrEmpty()) {
|
||||
scope.launch(dispatchers.io) {
|
||||
val params = RegisterDeviceToken.Params(token = token)
|
||||
registerDeviceToken.async(params).fold(
|
||||
onSuccess = {
|
||||
Timber.d("Successfully registered token: $token")
|
||||
},
|
||||
onFailure = { error ->
|
||||
Timber.w("Failed to register token: $token, error: $error")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
// Nothing to do here
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PREF_KEY = "prefs.device_token"
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue