diff --git a/vector/src/gplay/java/im/vector/app/push/fcm/FirebaseReceiver.kt b/vector/src/gplay/java/im/vector/app/push/fcm/FirebaseReceiver.kt index d744634991..92473caa1e 100644 --- a/vector/src/gplay/java/im/vector/app/push/fcm/FirebaseReceiver.kt +++ b/vector/src/gplay/java/im/vector/app/push/fcm/FirebaseReceiver.kt @@ -41,7 +41,7 @@ class FirebaseReceiver : FirebaseMessagingService() { Timber.d("New Firebase token") fcmHelper.storeFcmToken(token) if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) { - pushersManager.enqueueRegisterPusher(token, getString(R.string.default_push_gateway_http_url)) + pushersManager.enqueueRegisterPusher(token, getString(R.string.pusher_http_url)) } } diff --git a/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt b/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt index 91ab58207d..98412497fc 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt @@ -30,6 +30,7 @@ private const val DEFAULT_PUSHER_FILE_TAG = "mobile" class PushersManager @Inject constructor( private val unifiedPushStore: UnifiedPushStore, + private val unifiedPushHelper: UnifiedPushHelper, private val activeSessionHolder: ActiveSessionHolder, private val localeProvider: LocaleProvider, private val stringProvider: StringProvider, @@ -39,9 +40,9 @@ class PushersManager @Inject constructor( val currentSession = activeSessionHolder.getActiveSession() currentSession.pushersService().testPush( - unifiedPushStore.getPushGateway()!!, + unifiedPushStore.getPushGateway(), stringProvider.getString(R.string.pusher_app_id), - unifiedPushStore.getEndpointOrToken().orEmpty(), + unifiedPushHelper.getEndpointOrToken().orEmpty(), TEST_EVENT_ID ) } diff --git a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt index 0993485471..a190ed7843 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt @@ -184,7 +184,10 @@ class UnifiedPushHelper @Inject constructor( val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME vectorPreferences.setFdroidSyncBackgroundMode(mode) try { - pushersManager?.unregisterPusher(unifiedPushStore.getEndpointOrToken().orEmpty()) + getEndpointOrToken()?.let { + Timber.d("Removing $it") + pushersManager?.unregisterPusher(it) + } } catch (e: Exception) { Timber.d(e, "Probably unregistering a non existing pusher") } @@ -261,7 +264,7 @@ class UnifiedPushHelper @Inject constructor( } fun getPrivacyFriendlyUpEndpoint(): String? { - val endpoint = unifiedPushStore.getEndpointOrToken() + val endpoint = getEndpointOrToken() if (endpoint.isNullOrEmpty()) return null if (isEmbeddedDistributor()) { return endpoint @@ -274,4 +277,9 @@ class UnifiedPushHelper @Inject constructor( null } } + + fun getEndpointOrToken(): String? { + return if (isEmbeddedDistributor()) fcmHelper.getFcmToken() + else unifiedPushStore.getEndpoint() + } } diff --git a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushStore.kt b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushStore.kt index 07d291a723..86247cd749 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushStore.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushStore.kt @@ -18,11 +18,13 @@ package im.vector.app.core.pushers import android.content.Context import androidx.core.content.edit +import im.vector.app.R import im.vector.app.core.di.DefaultSharedPreferences import javax.inject.Inject class UnifiedPushStore @Inject constructor( - context: Context, + val context: Context, + val fcmHelper: FcmHelper ) { private val defaultPrefs = DefaultSharedPreferences.getInstance(context) @@ -31,7 +33,7 @@ class UnifiedPushStore @Inject constructor( * * @return the UnifiedPush Endpoint or null if not received */ - fun getEndpointOrToken(): String? { + fun getEndpoint(): String? { return defaultPrefs.getString(PREFS_ENDPOINT_OR_TOKEN, null) } @@ -51,8 +53,9 @@ class UnifiedPushStore @Inject constructor( * * @return the Push Gateway or null if not defined */ - fun getPushGateway(): String? { + fun getPushGateway(): String { return defaultPrefs.getString(PREFS_PUSH_GATEWAY, null) + ?: context.getString(R.string.pusher_http_url) } /** diff --git a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt index e99e3fdd16..4ee317bce7 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt @@ -65,11 +65,11 @@ class VectorMessagingReceiver : MessagingReceiver() { if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) { // If the endpoint has changed // or the gateway has changed - if (unifiedPushStore.getEndpointOrToken() != endpoint) { + if (unifiedPushHelper.getEndpointOrToken() != endpoint) { unifiedPushStore.storeUpEndpoint(endpoint) coroutineScope.launch { unifiedPushHelper.storeCustomOrDefaultGateway(endpoint) { - unifiedPushStore.getPushGateway()?.let { + unifiedPushStore.getPushGateway().let { pushersManager.enqueueRegisterPusher(endpoint, it) } } @@ -97,7 +97,7 @@ class VectorMessagingReceiver : MessagingReceiver() { guardServiceStarter.start() runBlocking { try { - pushersManager.unregisterPusher(unifiedPushStore.getEndpointOrToken().orEmpty()) + pushersManager.unregisterPusher(unifiedPushHelper.getEndpointOrToken().orEmpty()) } catch (e: Exception) { Timber.tag(loggerTag.value).d("Probably unregistering a non existing pusher") } diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt index 66222f759e..dbabe8a691 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt @@ -37,12 +37,11 @@ class TestEndpointAsTokenRegistration @Inject constructor( private val pushersManager: PushersManager, private val activeSessionHolder: ActiveSessionHolder, private val unifiedPushHelper: UnifiedPushHelper, - private val unifiedPushStore: UnifiedPushStore, ) : TroubleshootTest(R.string.settings_troubleshoot_test_endpoint_registration_title) { override fun perform(activityResultLauncher: ActivityResultLauncher) { // Check if we have a registered pusher for this token - val endpoint = unifiedPushStore.getEndpointOrToken() ?: run { + val endpoint = unifiedPushHelper.getEndpointOrToken() ?: run { status = TestStatus.FAILED return }