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

@@ -14,11 +14,6 @@ private val TAG = HuaweiPushService::class.java.simpleName
@AndroidEntryPoint
class HuaweiPushService: HmsMessageService() {
init {
Log.d(TAG, "init Huawei Service")
}
@Inject lateinit var pushRegistry: PushRegistry
@Inject lateinit var pushReceiver: PushReceiver
@@ -32,33 +27,13 @@ class HuaweiPushService: HmsMessageService() {
pushReceiver.onPush(message?.data?.let(Base64::decode))
}
override fun onMessageSent(p0: String?) {
Log.d(TAG, "onMessageSent() called with: p0 = $p0")
super.onMessageSent(p0)
}
override fun onSendError(p0: String?, p1: Exception?) {
Log.d(TAG, "onSendError() called with: p0 = $p0, p1 = $p1")
super.onSendError(p0, p1)
}
override fun onMessageDelivered(p0: String?, p1: Exception?) {
Log.d(TAG, "onMessageDelivered")
super.onMessageDelivered(p0, p1)
}
override fun onNewToken(p0: String?) {
Log.d(TAG, "onNewToken")
super.onNewToken(p0)
override fun onNewToken(token: String?) {
pushRegistry.register(token)
}
override fun onNewToken(token: String?, bundle: Bundle?) {
Log.d(TAG, "New HCM token: $token.")
TextSecurePreferences.setPushToken(this, token)
pushRegistry.refresh(token, true)
onNewToken(token)
}
override fun onDeletedMessages() {

View File

@@ -2,26 +2,28 @@ package org.thoughtcrime.securesms.notifications
import android.content.Context
import com.huawei.hms.aaid.HmsInstanceId
import dagger.Lazy
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.session.libsignal.utilities.Log
import javax.inject.Inject
import javax.inject.Singleton
private const val APP_ID = "107205081"
private const val TOKEN_SCOPE = "HCM"
@Singleton
class HuaweiTokenFetcher @Inject constructor(
@ApplicationContext private val context: Context
@ApplicationContext private val context: Context,
private val pushRegistry: Lazy<PushRegistry>,
): TokenFetcher {
override fun fetch(): Job {
val hmsInstanceId = HmsInstanceId.getInstance(context)
return MainScope().launch(Dispatchers.IO) {
val appId = "107205081"
val tokenScope = "HCM"
// getToken returns an empty string, but triggers the service to initialize.
hmsInstanceId.getToken(appId, tokenScope)
}
override suspend fun fetch(): String? = HmsInstanceId.getInstance(context).run {
// https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/push-basic-capability#h2-1576218800370
// getToken may return an empty string, if so HuaweiPushService#onNewToken will be called.
withContext(Dispatchers.IO) { getToken(APP_ID, TOKEN_SCOPE) }
}
}