Connect Huawei push notifications
parent
34990b13d3
commit
55216875ac
@ -1,9 +1,29 @@
|
|||||||
package org.thoughtcrime.securesms.notifications
|
package org.thoughtcrime.securesms.notifications
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import com.huawei.hms.aaid.HmsInstanceId
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
|
||||||
class HuaweiPushManager(val context: Context): PushManager {
|
class HuaweiPushManager(val context: Context): PushManager {
|
||||||
|
private var huaweiPushInstanceIdJob: Job? = null
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
override fun refresh(force: Boolean) {
|
override fun refresh(force: Boolean) {
|
||||||
|
val huaweiPushInstanceIdJob = huaweiPushInstanceIdJob
|
||||||
|
|
||||||
|
huaweiPushInstanceIdJob?.apply {
|
||||||
|
if (force) cancel() else if (isActive) return
|
||||||
|
}
|
||||||
|
|
||||||
|
val hmsInstanceId = HmsInstanceId.getInstance(context)
|
||||||
|
|
||||||
|
val task = hmsInstanceId.aaid
|
||||||
|
|
||||||
|
// HuaweiPushNotificationService().start()
|
||||||
|
//
|
||||||
|
// huaweiPushInstanceIdJob = HmsInstanceId.getInstance(this) { hmsInstanceId ->
|
||||||
|
// RegisterHuaweiPushService(hmsInstanceId, this, force).start()
|
||||||
|
// Unit.INSTANCE
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package org.thoughtcrime.securesms
|
||||||
|
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.Provides
|
||||||
|
import dagger.hilt.InstallIn
|
||||||
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
import network.loki.messenger.BuildConfig
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
object DeviceModule {
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provides() = BuildConfig.DEVICE
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package org.thoughtcrime.securesms.notifications
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.goterl.lazysodium.utils.KeyPair
|
||||||
|
import nl.komponents.kovenant.Promise
|
||||||
|
import nl.komponents.kovenant.combine.and
|
||||||
|
import org.session.libsession.messaging.sending_receiving.notifications.PushManagerV1
|
||||||
|
import org.session.libsession.utilities.Device
|
||||||
|
import org.session.libsession.utilities.TextSecurePreferences
|
||||||
|
import org.session.libsignal.utilities.Log
|
||||||
|
import org.session.libsignal.utilities.Namespace
|
||||||
|
import org.session.libsignal.utilities.emptyPromise
|
||||||
|
import org.thoughtcrime.securesms.crypto.KeyPairUtilities
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
private const val TAG = "GenericPushManager"
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class GenericPushManager @Inject constructor(
|
||||||
|
private val context: Context,
|
||||||
|
private val device: Device,
|
||||||
|
private val tokenManager: FcmTokenManager,
|
||||||
|
private val pushManagerV2: PushManagerV2,
|
||||||
|
) {
|
||||||
|
fun refresh(token: String, force: Boolean): Promise<*, Exception> {
|
||||||
|
Log.d(TAG, "refresh() called")
|
||||||
|
|
||||||
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context) ?: return emptyPromise()
|
||||||
|
val userEdKey = KeyPairUtilities.getUserED25519KeyPair(context) ?: return emptyPromise()
|
||||||
|
|
||||||
|
return when {
|
||||||
|
tokenManager.isUsingFCM -> register(force, token, userPublicKey, userEdKey)
|
||||||
|
tokenManager.requiresUnregister -> unregister(token, userPublicKey, userEdKey)
|
||||||
|
else -> emptyPromise()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register for push notifications if:
|
||||||
|
* force is true
|
||||||
|
* there is no FCM Token
|
||||||
|
* FCM Token has expired
|
||||||
|
*/
|
||||||
|
private fun register(
|
||||||
|
force: Boolean,
|
||||||
|
token: String,
|
||||||
|
publicKey: String,
|
||||||
|
userEd25519Key: KeyPair,
|
||||||
|
namespaces: List<Int> = listOf(Namespace.DEFAULT)
|
||||||
|
): Promise<*, Exception> = if (force || tokenManager.isInvalid()) {
|
||||||
|
register(token, publicKey, userEd25519Key, namespaces)
|
||||||
|
} else emptyPromise()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register for push notifications.
|
||||||
|
*/
|
||||||
|
private fun register(
|
||||||
|
token: String,
|
||||||
|
publicKey: String,
|
||||||
|
userEd25519Key: KeyPair,
|
||||||
|
namespaces: List<Int> = listOf(Namespace.DEFAULT)
|
||||||
|
): Promise<*, Exception> = PushManagerV1.register(
|
||||||
|
token = token,
|
||||||
|
device = device,
|
||||||
|
publicKey = publicKey
|
||||||
|
) and pushManagerV2.register(
|
||||||
|
token, publicKey, userEd25519Key, namespaces
|
||||||
|
) fail {
|
||||||
|
Log.e(TAG, "registerBoth failed", it)
|
||||||
|
} success {
|
||||||
|
Log.d(TAG, "registerBoth success... saving token!!")
|
||||||
|
tokenManager.fcmToken = token
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun unregister(
|
||||||
|
token: String,
|
||||||
|
userPublicKey: String,
|
||||||
|
userEdKey: KeyPair
|
||||||
|
): Promise<*, Exception> = PushManagerV1.unregister() and pushManagerV2.unregister(
|
||||||
|
token, userPublicKey, userEdKey
|
||||||
|
) fail {
|
||||||
|
Log.e(TAG, "unregisterBoth failed", it)
|
||||||
|
} success {
|
||||||
|
tokenManager.fcmToken = null
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package org.session.libsession.utilities
|
||||||
|
|
||||||
|
enum class Device(val value: String) {
|
||||||
|
ANDROID("android"),
|
||||||
|
HUAWEI("huawei");
|
||||||
|
}
|
Loading…
Reference in New Issue