2023-08-06 14:52:39 +02:00
|
|
|
package org.thoughtcrime.securesms.notifications
|
|
|
|
|
|
|
|
import com.google.android.gms.tasks.Tasks
|
|
|
|
import com.google.firebase.iid.FirebaseInstanceId
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.isActive
|
2023-08-09 02:38:42 +02:00
|
|
|
import kotlinx.coroutines.withContext
|
2023-08-06 14:52:39 +02:00
|
|
|
import javax.inject.Inject
|
|
|
|
import javax.inject.Singleton
|
|
|
|
|
|
|
|
@Singleton
|
2023-08-09 02:38:42 +02:00
|
|
|
class FirebaseTokenFetcher @Inject constructor(): TokenFetcher {
|
|
|
|
override suspend fun fetch() = withContext(Dispatchers.IO) {
|
2023-08-06 14:52:39 +02:00
|
|
|
FirebaseInstanceId.getInstance().instanceId
|
|
|
|
.also(Tasks::await)
|
2023-08-09 02:38:42 +02:00
|
|
|
.takeIf { isActive } // don't 'complete' task if we were canceled
|
|
|
|
?.run { result?.token ?: throw exception!! }
|
2023-08-06 14:52:39 +02:00
|
|
|
}
|
|
|
|
}
|