Skip to content

Commit

Permalink
Migrating from v4 to v5 checks for cached app ID
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
nan-li committed Jan 31, 2025
1 parent 1a6104f commit f572b8d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,23 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
sessionModel = services.getService<SessionModelStore>().model
operationRepo = services.getService<IOperationRepo>()

// 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) {
Expand Down Expand Up @@ -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: (
Expand Down

0 comments on commit f572b8d

Please sign in to comment.