From 2531e9dd6215871c6241dd936ee35021de490a45 Mon Sep 17 00:00:00 2001 From: Jonathan Almeida Date: Fri, 19 Jul 2019 16:05:17 -0400 Subject: [PATCH] No issue: Use correct server host and force subscribe for push --- .../org/mozilla/fenix/FenixApplication.kt | 8 ++++ .../fenix/components/BackgroundServices.kt | 40 ++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 07accb0b6..9cd583b39 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking import mozilla.components.concept.fetch.Client +import mozilla.components.concept.push.PushProcessor import mozilla.components.service.fretboard.Fretboard import mozilla.components.service.fretboard.source.kinto.KintoExperimentSource import mozilla.components.service.fretboard.storage.flatfile.FlatFileExperimentStorage @@ -69,6 +70,13 @@ open class FenixApplication : Application() { if (Settings.getInstance(this).isTelemetryEnabled) { components.analytics.metrics.start() } + + // Sets the PushFeature as the singleton instance for push messages to go to. + // We need the push feature setup here to deliver messages in the case where the service + // starts up the app first. + if (FeatureFlags.sendTabEnabled && components.backgroundServices.pushConfig != null) { + PushProcessor.install(components.backgroundServices.push) + } } private fun registerRxExceptionHandling() { diff --git a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt index 27b79ed97..f0217c65b 100644 --- a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt +++ b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt @@ -6,6 +6,7 @@ package org.mozilla.fenix.components import android.content.Context import android.os.Build +import android.preference.PreferenceManager import androidx.lifecycle.ProcessLifecycleOwner import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -13,7 +14,6 @@ import kotlinx.coroutines.launch import mozilla.components.browser.storage.sync.PlacesBookmarksStorage import mozilla.components.browser.storage.sync.PlacesHistoryStorage import mozilla.components.concept.push.Bus -import mozilla.components.concept.push.PushProcessor import mozilla.components.concept.sync.AccountObserver import mozilla.components.concept.sync.DeviceCapability import mozilla.components.concept.sync.DeviceEvent @@ -76,7 +76,7 @@ class BackgroundServices( SyncConfig(setOf("history", "bookmarks"), syncPeriodInMinutes = 240L) // four hours } - private val pushConfig by lazy { + val pushConfig by lazy { val projectIdKey = context.getString(R.string.pref_key_push_project_id) val resId = context.resources.getIdentifier(projectIdKey, "string", context.packageName) if (resId == 0) { @@ -88,7 +88,7 @@ class BackgroundServices( val pushService by lazy { FirebasePush() } - private val push by lazy { + val push by lazy { AutoPushFeature(context = context, service = pushService, config = pushConfig!!).also { // Notify observers for Services' messages. it.registerForPushMessages(PushType.Services, object : Bus.Observer { @@ -101,16 +101,31 @@ class BackgroundServices( // Notify observers for subscription changes. it.registerForSubscriptions(object : PushSubscriptionObserver { override fun onSubscriptionAvailable(subscription: AutoPushSubscription) { - accountManager.authenticatedAccount()?.deviceConstellation() - ?.setDevicePushSubscriptionAsync( - DevicePushSubscription( - endpoint = subscription.endpoint, - publicKey = subscription.publicKey, - authKey = subscription.authKey + // Update for only the services subscription. + if (subscription.type == PushType.Services) { + accountManager.authenticatedAccount()?.deviceConstellation() + ?.setDevicePushSubscriptionAsync( + DevicePushSubscription( + endpoint = subscription.endpoint, + publicKey = subscription.publicKey, + authKey = subscription.authKey + ) ) - ) + } } }, ProcessLifecycleOwner.get(), false) + + // For all the current Fenix users, we need to remove the current push token and + // re-subscribe again on the right push server. We should never do this otherwise! + // Should be removed after majority of our users are correctly subscribed. + // See: https://github.com/mozilla-mobile/fenix/issues/4218 + + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + val prefResetSubKey = "reset_broken_push_subscription" + if (preferences.getBoolean(prefResetSubKey, true)) { + preferences.edit().putBoolean(prefResetSubKey, false).apply() + it.forceRegistrationRenewal() + } } } @@ -118,11 +133,6 @@ class BackgroundServices( // Make the "history" and "bookmark" stores accessible to workers spawned by the sync manager. GlobalSyncableStoreProvider.configureStore("history" to historyStorage) GlobalSyncableStoreProvider.configureStore("bookmarks" to bookmarkStorage) - - // Sets the PushFeature as the singleton instance for push messages to go to. - if (FeatureFlags.sendTabEnabled && pushConfig != null) { - PushProcessor.install(push) - } } private val deviceEventObserver = object : DeviceEventsObserver {