@@ -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,36 @@ internal class InAppMessagesManager(
113
117
private val fetchIAMMutex = Mutex ()
114
118
private var lastTimeFetchedIAMs: Long? = null
115
119
120
+ private val identityModelChangeHandler =
121
+ object : ISingletonModelStoreChangeHandler <IdentityModel > {
122
+ override fun onModelReplaced (
123
+ model : IdentityModel ,
124
+ tag : String ,
125
+ ) { }
126
+
127
+ override fun onModelUpdated (
128
+ args : ModelChangedArgs ,
129
+ tag : String ,
130
+ ) {
131
+ if (args.property == IdentityConstants .ONESIGNAL_ID ) {
132
+ val oldOneSignalId = args.oldValue as String
133
+ val newOneSignalId = args.newValue as String
134
+
135
+ // Create a IAM fetch condition when a backend OneSignalID is retrieved for the first time
136
+ if (IDManager .isLocalId(oldOneSignalId) && ! IDManager .isLocalId(newOneSignalId)) {
137
+ suspendifyOnThread {
138
+ val updateConditionDeferred =
139
+ _consistencyManager .getRywDataFromAwaitableCondition(IamFetchReadyCondition (newOneSignalId))
140
+ val rywToken = updateConditionDeferred.await()
141
+ if (rywToken != null ) {
142
+ fetchMessages(rywToken)
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ }
149
+
116
150
override var paused: Boolean
117
151
get() = _state .paused
118
152
set(value) {
@@ -150,6 +184,7 @@ internal class InAppMessagesManager(
150
184
_triggerController .subscribe(this )
151
185
_sessionService .subscribe(this )
152
186
_applicationService .addApplicationLifecycleHandler(this )
187
+ _identityModelStore .subscribe(identityModelChangeHandler)
153
188
154
189
suspendifyOnThread {
155
190
_repository .cleanCachedInAppMessages()
@@ -161,15 +196,6 @@ internal class InAppMessagesManager(
161
196
for (redisplayInAppMessage in redisplayedInAppMessages) {
162
197
redisplayInAppMessage.isDisplayedInSession = false
163
198
}
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
199
}
174
200
}
175
201
0 commit comments