1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-3636 Notifications | Firebase device token (#2406)

This commit is contained in:
Konstantin Ivanov 2025-05-15 17:12:58 +02:00 committed by GitHub
parent 56e7dda7a2
commit d683042961
Signed by: github
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 17 deletions

View file

@ -16,6 +16,9 @@ dependencies {
implementation libs.timber
implementation platform(libs.firebaseBom)
implementation libs.firebaseMessaging
testImplementation libs.junit
testImplementation libs.kotlinTest
testImplementation libs.androidXTestCore

View file

@ -1,34 +1,42 @@
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 com.google.firebase.messaging.FirebaseMessaging
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()
}
}
proceedWithUpdatingToken(token = token)
}
override fun start() {
val token = sharedPreferences.getString(PREF_KEY, null)
scope.launch(dispatchers.io) {
FirebaseMessaging.getInstance().token
.addOnSuccessListener { token ->
if (token.isNotEmpty()) {
proceedWithUpdatingToken(token = token)
} else {
Timber.w("Firebase token is empty")
}
}
.addOnFailureListener { exception ->
Timber.w("Failed to get Firebase token: ${exception.message}")
}
}
}
private fun proceedWithUpdatingToken(token: String?) {
if (!token.isNullOrEmpty()) {
scope.launch(dispatchers.io) {
val params = RegisterDeviceToken.Params(token = token)
@ -47,8 +55,4 @@ class DeviceTokenStoringServiceImpl @Inject constructor(
override fun stop() {
// Nothing to do here
}
companion object {
private const val PREF_KEY = "prefs.device_token"
}
}