From b3d9c400083bfe8141f9b3cfaee73d12cb4c8dce Mon Sep 17 00:00:00 2001 From: Nan Date: Mon, 13 Jan 2025 11:05:18 -0800 Subject: [PATCH] Migrating from v4 to v5 checks for cached app ID * In wrappers, after an app upgrades from player to user model, if the app is not opened yet and a notification is received, there was no app ID detected and init returns early. * Now, check for the cached app ID from v4 and use that to continue the init process. This mimics what should happen when using the native SDK directly. * The reason this is an issue on wrappers is because when an app upgrades, the Application `onCreate` is called, without needing to open the app. However, in wrappers, the `onCreate` does not set app ID. --- .../preferences/IPreferencesService.kt | 5 ++++ .../com/onesignal/internal/OneSignalImp.kt | 26 ++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/IPreferencesService.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/IPreferencesService.kt index 23435d8977..f4d4b92a5d 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/IPreferencesService.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/IPreferencesService.kt @@ -177,6 +177,11 @@ object PreferencePlayerPurchasesKeys { object PreferenceOneSignalKeys { // Legacy + /** + * (String) The legacy app ID from SDKs prior to 5. + */ + const val PREFS_LEGACY_APP_ID = "GT_APP_ID" + /** * (String) The legacy player ID from SDKs prior to 5. */ diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt index 7babf5067b..5e36f3be39 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt @@ -211,15 +211,23 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { sessionModel = services.getService().model operationRepo = services.getService() - // initWithContext is called by our internal services/receivers/activites but they do not provide + var forceCreateUser = false + + // initWithContext is called by our internal services/receivers/activities but they do not provide // an appId (they don't know it). If the app has never called the external initWithContext // prior to our services/receivers/activities we will blow up, as no appId has been established. if (appId == null && !configModel!!.hasProperty(ConfigModel::appId.name)) { - Logging.warn("initWithContext called without providing appId, and no appId has been established!") - return false + val legacyAppId = getLegacyAppId() + if (legacyAppId == null) { + Logging.warn("initWithContext called without providing appId, and no appId has been established!") + return false + } else { + Logging.debug("initWithContext: using cached legacy appId $legacyAppId") + forceCreateUser = true + configModel!!.appId = legacyAppId + } } - var forceCreateUser = false // if the app id was specified as input, update the config model with it if (appId != null) { if (!configModel!!.hasProperty(ConfigModel::appId.name) || configModel!!.appId != appId) { @@ -418,6 +426,16 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { } } + /** + * Returns the cached app ID from v4 of the SDK, if available. + */ + private fun getLegacyAppId(): String? { + return preferencesService.getString( + PreferenceStores.ONESIGNAL, + PreferenceOneSignalKeys.PREFS_LEGACY_APP_ID, + ) + } + private fun createAndSwitchToNewUser( suppressBackendOperation: Boolean = false, modify: (