Skip to content

Commit c96e91f

Browse files
authored
Merge pull request #2244 from OneSignal/v4_to_v5_upgrade_migrate_app_id
V4 to v5 upgrade will migrate app ID for wrapper SDKs
2 parents 9cf7cd0 + f572b8d commit c96e91f

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/IPreferencesService.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ object PreferencePlayerPurchasesKeys {
177177
object PreferenceOneSignalKeys {
178178
// Legacy
179179

180+
/**
181+
* (String) The legacy app ID from SDKs prior to 5.
182+
*/
183+
const val PREFS_LEGACY_APP_ID = "GT_APP_ID"
184+
180185
/**
181186
* (String) The legacy player ID from SDKs prior to 5.
182187
*/

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,23 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
211211
sessionModel = services.getService<SessionModelStore>().model
212212
operationRepo = services.getService<IOperationRepo>()
213213

214-
// initWithContext is called by our internal services/receivers/activites but they do not provide
214+
var forceCreateUser = false
215+
216+
// initWithContext is called by our internal services/receivers/activities but they do not provide
215217
// an appId (they don't know it). If the app has never called the external initWithContext
216218
// prior to our services/receivers/activities we will blow up, as no appId has been established.
217219
if (appId == null && !configModel!!.hasProperty(ConfigModel::appId.name)) {
218-
Logging.warn("initWithContext called without providing appId, and no appId has been established!")
219-
return false
220+
val legacyAppId = getLegacyAppId()
221+
if (legacyAppId == null) {
222+
Logging.warn("initWithContext called without providing appId, and no appId has been established!")
223+
return false
224+
} else {
225+
Logging.debug("initWithContext: using cached legacy appId $legacyAppId")
226+
forceCreateUser = true
227+
configModel!!.appId = legacyAppId
228+
}
220229
}
221230

222-
var forceCreateUser = false
223231
// if the app id was specified as input, update the config model with it
224232
if (appId != null) {
225233
if (!configModel!!.hasProperty(ConfigModel::appId.name) || configModel!!.appId != appId) {
@@ -418,6 +426,16 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
418426
}
419427
}
420428

429+
/**
430+
* Returns the cached app ID from v4 of the SDK, if available.
431+
*/
432+
private fun getLegacyAppId(): String? {
433+
return preferencesService.getString(
434+
PreferenceStores.ONESIGNAL,
435+
PreferenceOneSignalKeys.PREFS_LEGACY_APP_ID,
436+
)
437+
}
438+
421439
private fun createAndSwitchToNewUser(
422440
suppressBackendOperation: Boolean = false,
423441
modify: (

0 commit comments

Comments
 (0)