Fixed huawei push notification

This commit is contained in:
SessionHero01 2024-10-25 10:48:22 +11:00
parent 0518134c88
commit 5b26912f42
No known key found for this signature in database
4 changed files with 29 additions and 25 deletions

View File

@ -4,6 +4,7 @@ import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.session.libsession.messaging.notifications.TokenFetcher
@Module
@InstallIn(SingletonComponent::class)

View File

@ -4,18 +4,16 @@ import android.os.Bundle
import com.huawei.hms.push.HmsMessageService
import com.huawei.hms.push.RemoteMessage
import dagger.hilt.android.AndroidEntryPoint
import org.json.JSONException
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.messaging.notifications.TokenFetcher
import org.session.libsignal.utilities.Base64
import org.session.libsignal.utilities.Log
import java.lang.Exception
import javax.inject.Inject
private val TAG = HuaweiPushService::class.java.simpleName
@AndroidEntryPoint
class HuaweiPushService: HmsMessageService() {
@Inject lateinit var pushRegistry: PushRegistry
@Inject lateinit var tokenFetcher: TokenFetcher
@Inject lateinit var pushReceiver: PushReceiver
override fun onMessageReceived(message: RemoteMessage?) {
@ -25,16 +23,15 @@ class HuaweiPushService: HmsMessageService() {
}
override fun onNewToken(token: String?) {
pushRegistry.register(token)
if (token != null) {
tokenFetcher.onNewToken(token)
}
}
override fun onNewToken(token: String?, bundle: Bundle?) {
Log.d(TAG, "New HCM token: $token.")
pushRegistry.register(token)
}
override fun onDeletedMessages() {
Log.d(TAG, "onDeletedMessages")
pushRegistry.refresh(false)
if (token != null) {
tokenFetcher.onNewToken(token)
}
}
}

View File

@ -2,14 +2,13 @@ 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.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.session.libsignal.utilities.Log
import org.session.libsession.messaging.notifications.TokenFetcher
import javax.inject.Inject
import javax.inject.Singleton
@ -19,13 +18,20 @@ private const val TOKEN_SCOPE = "HCM"
@Singleton
class HuaweiTokenFetcher @Inject constructor(
@ApplicationContext private val context: Context,
private val pushRegistry: Lazy<PushRegistry>,
): TokenFetcher {
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)
override val token = MutableStateFlow<String?>(null)
override fun onNewToken(token: String) {
this.token.value = token
}
init {
GlobalScope.launch {
val instanceId = HmsInstanceId.getInstance(context)
withContext(Dispatchers.Default) {
instanceId.getToken(APP_ID, TOKEN_SCOPE)
}
}
}
}

View File

@ -13,7 +13,6 @@ import kotlinx.serialization.json.jsonObject
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.session.libsession.messaging.notifications.TokenFetcher
import org.session.libsession.messaging.sending_receiving.notifications.Response
import org.session.libsession.messaging.sending_receiving.notifications.Server
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionRequest
@ -21,7 +20,7 @@ import org.session.libsession.messaging.sending_receiving.notifications.Subscrip
import org.session.libsession.messaging.sending_receiving.notifications.UnsubscribeResponse
import org.session.libsession.messaging.sending_receiving.notifications.UnsubscriptionRequest
import org.session.libsession.snode.OnionRequestAPI
import org.session.libsession.snode.SnodeAPI
import org.session.libsession.snode.SnodeClock
import org.session.libsession.snode.SwarmAuth
import org.session.libsession.snode.Version
import org.session.libsession.snode.utilities.await
@ -36,6 +35,7 @@ private const val maxRetryCount = 4
class PushRegistryV2 @Inject constructor(
private val pushReceiver: PushReceiver,
private val device: Device,
private val clock: SnodeClock,
) {
suspend fun register(
token: String,
@ -44,7 +44,7 @@ class PushRegistryV2 @Inject constructor(
) {
val pnKey = pushReceiver.getOrCreateNotificationKey()
val timestamp = SnodeAPI.nowWithOffset / 1000 // get timestamp in ms -> s
val timestamp = clock.currentTimeMills() / 1000 // get timestamp in ms -> s
val publicKey = swarmAuth.accountId.hexString
val signed = swarmAuth.sign(
"MONITOR${publicKey}${timestamp}1${namespaces.joinToString(separator = ",")}".encodeToByteArray()
@ -75,7 +75,7 @@ class PushRegistryV2 @Inject constructor(
swarmAuth: SwarmAuth
) {
val publicKey = swarmAuth.accountId.hexString
val timestamp = SnodeAPI.nowWithOffset / 1000 // get timestamp in ms -> s
val timestamp = clock.currentTimeMills() / 1000 // get timestamp in ms -> s
// if we want to support passing namespace list, here is the place to do it
val signature = swarmAuth.signForPushRegistry(
"UNSUBSCRIBE${publicKey}${timestamp}".encodeToByteArray()