From 5f5b873414125afa348019c286dd5e38e04647e6 Mon Sep 17 00:00:00 2001 From: S1m Date: Mon, 7 Jun 2021 13:22:07 +0200 Subject: [PATCH] different Pusher AppId --- ...tificationTroubleshootTestManagerFactory.kt | 2 +- ...tificationTroubleshootTestManagerFactory.kt | 2 +- .../vector/app/core/pushers/PushersManager.kt | 18 +++++++++++++----- .../im/vector/app/core/pushers/UPHelper.kt | 8 +++++++- .../core/pushers/VectorMessagingReceiver.kt | 4 ++-- ...orSettingsNotificationPreferenceFragment.kt | 2 +- .../troubleshoot/TestTokenRegistration.kt | 2 +- 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt b/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt index 366128ff2c..4a73457c01 100644 --- a/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt +++ b/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt @@ -51,7 +51,7 @@ class NotificationTroubleshootTestManagerFactory @Inject constructor( mgr.addTest(testSystemSettings) mgr.addTest(testAccountSettings) mgr.addTest(testDeviceSettings) - if (UPHelper.DistributorExists(fragment.requireContext())) { + if (UPHelper.distributorExists(fragment.requireContext())) { mgr.addTest(testBingRulesSettings) mgr.addTest(testNewEndpoint) mgr.addTest(testTokenRegistration) diff --git a/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt b/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt index 14c0e1c48d..36afcb28f3 100644 --- a/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt +++ b/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt @@ -53,7 +53,7 @@ class NotificationTroubleshootTestManagerFactory @Inject constructor( mgr.addTest(testSystemSettings) mgr.addTest(testAccountSettings) mgr.addTest(testDeviceSettings) - if (UPHelper.DistributorExists(fragment.requireContext())) { + if (UPHelper.distributorExists(fragment.requireContext())) { mgr.addTest(testBingRulesSettings) mgr.addTest(testPlayServices) mgr.addTest(testNewEndpoint) 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 7c5e38c8fe..6462bcf3c8 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 @@ -39,19 +39,19 @@ class PushersManager @Inject constructor( currentSession.testPush( UPHelper.getPushGateway(context)!!, - stringProvider.getString(R.string.pusher_app_id), + getPusherAppId(context), UPHelper.getUpEndpoint(context)!!, TEST_EVENT_ID ) } - fun registerPusher(pushKey: String, gateway: String): UUID { + fun registerPusher(context: Context, pushKey: String, gateway: String): UUID { val currentSession = activeSessionHolder.getActiveSession() val profileTag = DEFAULT_PUSHER_FILE_TAG + "_" + abs(currentSession.myUserId.hashCode()) return currentSession.addHttpPusher( pushKey, // this is the UnifiedPush endpoint - stringProvider.getString(R.string.pusher_app_id), + getPusherAppId(context), profileTag, localeProvider.current().language, appNameProvider.getAppName(), @@ -62,9 +62,17 @@ class PushersManager @Inject constructor( ) } - suspend fun unregisterPusher(pushKey: String) { + suspend fun unregisterPusher(context: Context, pushKey: String) { val currentSession = activeSessionHolder.getSafeActiveSession() ?: return - currentSession.removeHttpPusher(pushKey, stringProvider.getString(R.string.pusher_app_id)) + currentSession.removeHttpPusher(pushKey, getPusherAppId(context)) + } + + private fun getPusherAppId(context: Context) : String { + val appId = stringProvider.getString(R.string.pusher_app_id) + return if (UPHelper.isEmbeddedDistributor(context)) { + "${appId}.fcm" + } else + "${appId}.up" } companion object { diff --git a/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt b/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt index 4de1afc716..0d7e69d382 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt @@ -153,8 +153,14 @@ object UPHelper { return false } - fun DistributorExists(context: Context): Boolean { + fun distributorExists(context: Context): Boolean { val up = Registration() return up.getDistributor(context).isNotEmpty() } + + fun isEmbeddedDistributor(context: Context) : Boolean { + val up = Registration() + return up.getDistributor(context) == context.packageName + } + } 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 f54c5db661..c9fa4bfca1 100755 --- a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt @@ -151,7 +151,7 @@ val upHandler = object: MessagingReceiverHandler { val gateway = UPHelper.customOrDefaultGateway(context, endpoint) UPHelper.storePushGateway(context, gateway) UPHelper.storeUpEndpoint(context, endpoint) - pusherManager.registerPusher(endpoint, gateway) + pusherManager.registerPusher(context, endpoint, gateway) } val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED vectorPreferences.setFdroidSyncBackgroundMode(mode) @@ -172,7 +172,7 @@ val upHandler = object: MessagingReceiverHandler { vectorPreferences.setFdroidSyncBackgroundMode(mode) runBlocking { try { - pusherManager.unregisterPusher(UPHelper.getUpEndpoint(context)!!) + pusherManager.unregisterPusher(context, UPHelper.getUpEndpoint(context)!!) } catch (e: Exception) { Timber.d("Probably unregistering a non existant pusher") } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt index a798efc168..e4cd8db693 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt @@ -296,7 +296,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( lifecycleScope.launch { runCatching { try { - pushManager.unregisterPusher(it) + pushManager.unregisterPusher(requireContext(), it) } catch (e: Exception) { Timber.d("Probably unregistering a non existant pusher") } diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestTokenRegistration.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestTokenRegistration.kt index b9ca1290ff..eb92ed00af 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestTokenRegistration.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestTokenRegistration.kt @@ -60,7 +60,7 @@ class TestTokenRegistration @Inject constructor(private val context: AppCompatAc stringProvider.getString(R.string.sas_error_unknown)) quickFix = object : TroubleshootQuickFix(R.string.settings_troubleshoot_test_token_registration_quick_fix) { override fun doFix() { - val workId = pushersManager.registerPusher(pushToken, pushGateway) + val workId = pushersManager.registerPusher(context, pushToken, pushGateway) WorkManager.getInstance(context).getWorkInfoByIdLiveData(workId).observe(context, Observer { workInfo -> if (workInfo != null) { if (workInfo.state == WorkInfo.State.SUCCEEDED) {