Refactor to accept Huawei token from getToken() and/or onNewToken()

This commit is contained in:
andrew
2023-08-09 10:08:42 +09:30
parent c8dcfbf32c
commit 5a5b2f593f
10 changed files with 59 additions and 148 deletions

View File

@@ -18,7 +18,7 @@ class FirebasePushService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
if (token == prefs.getPushToken()) return
pushRegistry.refresh(token, true)
pushRegistry.register(token)
}
override fun onMessageReceived(message: RemoteMessage) {

View File

@@ -1,43 +1,19 @@
package org.thoughtcrime.securesms.notifications
import com.google.android.gms.tasks.Task
import com.google.android.gms.tasks.Tasks
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.iid.InstanceIdResult
import dagger.Lazy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import org.session.libsignal.utilities.Log
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
private val TAG = FirebaseTokenFetcher::class.java.name
@Singleton
class FirebaseTokenFetcher @Inject constructor(
private val pushRegistry: Lazy<PushRegistry>,
): TokenFetcher {
override fun fetch(): Job = MainScope().launch(Dispatchers.IO) {
class FirebaseTokenFetcher @Inject constructor(): TokenFetcher {
override suspend fun fetch() = withContext(Dispatchers.IO) {
FirebaseInstanceId.getInstance().instanceId
.also(Tasks::await)
.also { if (!isActive) return@launch } // don't 'complete' task if we were canceled
.process()
.takeIf { isActive } // don't 'complete' task if we were canceled
?.run { result?.token ?: throw exception!! }
}
private fun Task<InstanceIdResult>.process() = when {
isSuccessful -> try {
result?.token?.let {
pushRegistry.get().refresh(it, force = true).get()
}
} catch (e: Exception) {
onFail(e)
}
else -> exception?.let(::onFail)
}
private fun onFail(e: Exception) = Log.e(TAG, "fetch failed", e)
}