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..d739e28cd4 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,27 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { sessionModel = services.getService().model operationRepo = services.getService() + var forceCreateUser = false + // initWithContext is called by our internal services/receivers/activites 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 = + preferencesService!!.getString( + PreferenceStores.ONESIGNAL, + PreferenceOneSignalKeys.PREFS_LEGACY_APP_ID, + ) + 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) { diff --git a/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/internal/OneSignalImpTests.kt b/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/internal/OneSignalImpTests.kt index 891139a411..1ee3b481f4 100644 --- a/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/internal/OneSignalImpTests.kt +++ b/OneSignalSDK/onesignal/core/src/test/java/com/onesignal/internal/OneSignalImpTests.kt @@ -1,5 +1,8 @@ package com.onesignal.internal +import android.content.Context +import androidx.test.core.app.ApplicationProvider +// import com.onesignal.notifications.NotificationsModule import com.onesignal.debug.LogLevel import com.onesignal.debug.internal.logging.Logging import io.kotest.assertions.throwables.shouldThrowUnit @@ -86,4 +89,22 @@ class OneSignalImpTests : FunSpec({ } } } + + context("initWithContext") { + context("called without appId") { + test("has cached legacy appId should initialize") { + val os = OneSignalImp() + val context = ApplicationProvider.getApplicationContext() + + os.initWithContext(context, null) + + + } + test("does not have cached legacy appId should return early") { + // Given + // When + // Then + } + } + } })