mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-05 06:11:52 +00:00
Refactor to accept Huawei token from getToken() and/or onNewToken()
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user