diff --git a/app/src/main/java/com/anytypeio/anytype/di/main/SubscriptionsModule.kt b/app/src/main/java/com/anytypeio/anytype/di/main/SubscriptionsModule.kt index 111da9952a..01fa07336f 100644 --- a/app/src/main/java/com/anytypeio/anytype/di/main/SubscriptionsModule.kt +++ b/app/src/main/java/com/anytypeio/anytype/di/main/SubscriptionsModule.kt @@ -1,6 +1,5 @@ package com.anytypeio.anytype.di.main -import android.content.SharedPreferences import com.anytypeio.anytype.core_utils.di.scope.PerScreen import com.anytypeio.anytype.device.DeviceTokenStoringServiceImpl import com.anytypeio.anytype.di.main.ConfigModule.DEFAULT_APP_COROUTINE_SCOPE @@ -270,12 +269,10 @@ object SubscriptionsModule { @Provides @Singleton fun deviceTokenStoreService( - @Named("encrypted") sharedPreferences: SharedPreferences, registerDeviceToken: RegisterDeviceToken, dispatchers: AppCoroutineDispatchers, @Named(DEFAULT_APP_COROUTINE_SCOPE) scope: CoroutineScope ): DeviceTokenStoringService = DeviceTokenStoringServiceImpl( - sharedPreferences = sharedPreferences, registerDeviceToken = registerDeviceToken, dispatchers = dispatchers, scope = scope diff --git a/device/build.gradle b/device/build.gradle index 0ce221cd09..4b067d6751 100644 --- a/device/build.gradle +++ b/device/build.gradle @@ -16,6 +16,9 @@ dependencies { implementation libs.timber + implementation platform(libs.firebaseBom) + implementation libs.firebaseMessaging + testImplementation libs.junit testImplementation libs.kotlinTest testImplementation libs.androidXTestCore diff --git a/device/src/main/java/com/anytypeio/anytype/device/DeviceTokenStoringServiceImpl.kt b/device/src/main/java/com/anytypeio/anytype/device/DeviceTokenStoringServiceImpl.kt index 65b8441bcb..2a63c0f834 100644 --- a/device/src/main/java/com/anytypeio/anytype/device/DeviceTokenStoringServiceImpl.kt +++ b/device/src/main/java/com/anytypeio/anytype/device/DeviceTokenStoringServiceImpl.kt @@ -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" - } } \ No newline at end of file