Skip to content

Fix: IAM not shown when calling login immediately after init #2287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface IIdentityBackendService {
)
}

internal object IdentityConstants {
object IdentityConstants {
/**
* The alias label for the external ID alias.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import com.onesignal.session.internal.outcomes.IOutcomeEventsController
import com.onesignal.session.internal.session.ISessionLifecycleHandler
import com.onesignal.session.internal.session.ISessionService
import com.onesignal.user.IUserManager
import com.onesignal.user.internal.backend.IdentityConstants
import com.onesignal.user.internal.identity.IdentityModel
import com.onesignal.user.internal.identity.IdentityModelStore
import com.onesignal.user.internal.subscriptions.ISubscriptionChangedHandler
import com.onesignal.user.internal.subscriptions.ISubscriptionManager
import com.onesignal.user.internal.subscriptions.SubscriptionModel
Expand All @@ -60,6 +63,7 @@ internal class InAppMessagesManager(
private val _influenceManager: IInfluenceManager,
private val _configModelStore: ConfigModelStore,
private val _userManager: IUserManager,
private val _identityModelStore: IdentityModelStore,
private val _subscriptionManager: ISubscriptionManager,
private val _outcomeEventsController: IOutcomeEventsController,
private val _state: InAppStateService,
Expand Down Expand Up @@ -113,6 +117,36 @@ internal class InAppMessagesManager(
private val fetchIAMMutex = Mutex()
private var lastTimeFetchedIAMs: Long? = null

private val identityModelChangeHandler =
object : ISingletonModelStoreChangeHandler<IdentityModel> {
override fun onModelReplaced(
model: IdentityModel,
tag: String,
) { }

override fun onModelUpdated(
args: ModelChangedArgs,
tag: String,
) {
if (args.property == IdentityConstants.ONESIGNAL_ID) {
val oldOneSignalId = args.oldValue as String
val newOneSignalId = args.newValue as String

// Create a IAM fetch condition when a backend OneSignalID is retrieved for the first time
if (IDManager.isLocalId(oldOneSignalId) && !IDManager.isLocalId(newOneSignalId)) {
suspendifyOnThread {
val updateConditionDeferred =
_consistencyManager.getRywDataFromAwaitableCondition(IamFetchReadyCondition(newOneSignalId))
val rywToken = updateConditionDeferred.await()
if (rywToken != null) {
fetchMessages(rywToken)
}
}
}
}
}
}

override var paused: Boolean
get() = _state.paused
set(value) {
Expand Down Expand Up @@ -150,6 +184,7 @@ internal class InAppMessagesManager(
_triggerController.subscribe(this)
_sessionService.subscribe(this)
_applicationService.addApplicationLifecycleHandler(this)
_identityModelStore.subscribe(identityModelChangeHandler)

suspendifyOnThread {
_repository.cleanCachedInAppMessages()
Expand All @@ -161,15 +196,6 @@ internal class InAppMessagesManager(
for (redisplayInAppMessage in redisplayedInAppMessages) {
redisplayInAppMessage.isDisplayedInSession = false
}

// attempt to fetch messages from the backend (if we have the pre-requisite data already)
val onesignalId = _userManager.onesignalId
val updateConditionDeferred =
_consistencyManager.getRywDataFromAwaitableCondition(IamFetchReadyCondition(onesignalId))
val rywToken = updateConditionDeferred.await()
if (rywToken != null) {
fetchMessages(rywToken)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class InAppMessagesManagerTests : FunSpec({
mockk<IInfluenceManager>(),
mockk<ConfigModelStore>(),
mockk<IUserManager>(),
MockHelper.identityModelStore(),
mockk<ISubscriptionManager>(),
mockk<IOutcomeEventsController>(),
mockk<InAppStateService>(),
Expand Down
Loading