@@ -43,6 +43,9 @@ import com.onesignal.session.internal.outcomes.IOutcomeEventsController
43
43
import com.onesignal.session.internal.session.ISessionLifecycleHandler
44
44
import com.onesignal.session.internal.session.ISessionService
45
45
import com.onesignal.user.IUserManager
46
+ import com.onesignal.user.internal.backend.IdentityConstants
47
+ import com.onesignal.user.internal.identity.IdentityModel
48
+ import com.onesignal.user.internal.identity.IdentityModelStore
46
49
import com.onesignal.user.internal.subscriptions.ISubscriptionChangedHandler
47
50
import com.onesignal.user.internal.subscriptions.ISubscriptionManager
48
51
import com.onesignal.user.internal.subscriptions.SubscriptionModel
@@ -60,6 +63,7 @@ internal class InAppMessagesManager(
60
63
private val _influenceManager : IInfluenceManager ,
61
64
private val _configModelStore : ConfigModelStore ,
62
65
private val _userManager : IUserManager ,
66
+ private val _identityModelStore : IdentityModelStore ,
63
67
private val _subscriptionManager : ISubscriptionManager ,
64
68
private val _outcomeEventsController : IOutcomeEventsController ,
65
69
private val _state : InAppStateService ,
@@ -113,6 +117,29 @@ internal class InAppMessagesManager(
113
117
private val fetchIAMMutex = Mutex ()
114
118
private var lastTimeFetchedIAMs: Long? = null
115
119
120
+ private val identityModelChangeHandler = object : ISingletonModelStoreChangeHandler <IdentityModel > {
121
+ override fun onModelReplaced (model : IdentityModel , tag : String ) { }
122
+
123
+ override fun onModelUpdated (args : ModelChangedArgs , tag : String ) {
124
+ if (args.property == IdentityConstants .ONESIGNAL_ID ) {
125
+ val oldOneSignalId = args.oldValue as String
126
+ val newOneSignalId = args.newValue as String
127
+
128
+ // Create a IAM fetch condition when a backend OneSignalID is retrieved for the first time
129
+ if (IDManager .isLocalId(oldOneSignalId) && ! IDManager .isLocalId(newOneSignalId)) {
130
+ suspendifyOnThread {
131
+ val updateConditionDeferred =
132
+ _consistencyManager .getRywDataFromAwaitableCondition(IamFetchReadyCondition (newOneSignalId))
133
+ val rywToken = updateConditionDeferred.await()
134
+ if (rywToken != null ) {
135
+ fetchMessages(rywToken)
136
+ }
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+
116
143
override var paused: Boolean
117
144
get() = _state .paused
118
145
set(value) {
@@ -150,6 +177,7 @@ internal class InAppMessagesManager(
150
177
_triggerController .subscribe(this )
151
178
_sessionService .subscribe(this )
152
179
_applicationService .addApplicationLifecycleHandler(this )
180
+ _identityModelStore .subscribe(identityModelChangeHandler)
153
181
154
182
suspendifyOnThread {
155
183
_repository .cleanCachedInAppMessages()
@@ -161,15 +189,6 @@ internal class InAppMessagesManager(
161
189
for (redisplayInAppMessage in redisplayedInAppMessages) {
162
190
redisplayInAppMessage.isDisplayedInSession = false
163
191
}
164
-
165
- // attempt to fetch messages from the backend (if we have the pre-requisite data already)
166
- val onesignalId = _userManager .onesignalId
167
- val updateConditionDeferred =
168
- _consistencyManager .getRywDataFromAwaitableCondition(IamFetchReadyCondition (onesignalId))
169
- val rywToken = updateConditionDeferred.await()
170
- if (rywToken != null ) {
171
- fetchMessages(rywToken)
172
- }
173
192
}
174
193
}
175
194
@@ -933,3 +952,4 @@ internal class InAppMessagesManager(
933
952
934
953
override fun onUnfocused () { }
935
954
}
955
+
0 commit comments