@@ -6,6 +6,7 @@ import com.onesignal.common.IDManager
6
6
import com.onesignal.common.OneSignalUtils
7
7
import com.onesignal.common.modeling.ModelChangeTags
8
8
import com.onesignal.common.modules.IModule
9
+ import com.onesignal.common.safeString
9
10
import com.onesignal.common.services.IServiceProvider
10
11
import com.onesignal.common.services.ServiceBuilder
11
12
import com.onesignal.common.services.ServiceProvider
@@ -15,6 +16,9 @@ import com.onesignal.core.internal.application.impl.ApplicationService
15
16
import com.onesignal.core.internal.config.ConfigModel
16
17
import com.onesignal.core.internal.config.ConfigModelStore
17
18
import com.onesignal.core.internal.operations.IOperationRepo
19
+ import com.onesignal.core.internal.preferences.IPreferencesService
20
+ import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys
21
+ import com.onesignal.core.internal.preferences.PreferenceStores
18
22
import com.onesignal.core.internal.startup.StartupService
19
23
import com.onesignal.debug.IDebugManager
20
24
import com.onesignal.debug.LogLevel
@@ -32,6 +36,7 @@ import com.onesignal.user.UserModule
32
36
import com.onesignal.user.internal.backend.IdentityConstants
33
37
import com.onesignal.user.internal.identity.IdentityModel
34
38
import com.onesignal.user.internal.identity.IdentityModelStore
39
+ import com.onesignal.user.internal.operations.LoginUserFromSubscriptionOperation
35
40
import com.onesignal.user.internal.operations.LoginUserOperation
36
41
import com.onesignal.user.internal.operations.RefreshUserOperation
37
42
import com.onesignal.user.internal.properties.PropertiesModel
@@ -43,6 +48,7 @@ import com.onesignal.user.internal.subscriptions.SubscriptionType
43
48
import kotlinx.coroutines.sync.Mutex
44
49
import kotlinx.coroutines.sync.withLock
45
50
import kotlinx.coroutines.yield
51
+ import org.json.JSONObject
46
52
47
53
internal class OneSignalImp : IOneSignal , IServiceProvider {
48
54
override val sdkVersion: String = OneSignalUtils .sdkVersion
@@ -88,6 +94,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
88
94
private var _propertiesModelStore : PropertiesModelStore ? = null
89
95
private var _subscriptionModelStore : SubscriptionModelStore ? = null
90
96
private var _startupService : StartupService ? = null
97
+ private var _preferencesService : IPreferencesService ? = null
91
98
92
99
// Other State
93
100
private val _services : ServiceProvider
@@ -183,14 +190,48 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
183
190
_propertiesModelStore = _services .getService()
184
191
_identityModelStore = _services .getService()
185
192
_subscriptionModelStore = _services .getService()
193
+ _preferencesService = _services .getService()
186
194
187
195
// Instantiate and call the IStartableServices
188
196
_startupService = _services .getService()
189
197
_startupService !! .bootstrap()
190
198
191
199
if (forceCreateUser || ! _identityModelStore !! .model.hasProperty(IdentityConstants .ONESIGNAL_ID )) {
192
- createAndSwitchToNewUser()
193
- _operationRepo !! .enqueue(LoginUserOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId, _identityModelStore !! .model.externalId))
200
+ val legacyPlayerId = _preferencesService !! .getString(PreferenceStores .ONESIGNAL , PreferenceOneSignalKeys .PREFS_LEGACY_PLAYER_ID )
201
+ if (legacyPlayerId == null ) {
202
+ Logging .debug(" initWithContext: creating new device-scoped user" )
203
+ createAndSwitchToNewUser()
204
+ _operationRepo !! .enqueue(LoginUserOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId, _identityModelStore !! .model.externalId))
205
+ }
206
+ else {
207
+ Logging .debug(" initWithContext: creating user linked to subscription $legacyPlayerId " )
208
+
209
+ // Converting a 4.x SDK to the 5.x SDK. We pull the legacy user sync values to create the subscription model, then enqueue
210
+ // a specialized `LoginUserFromSubscriptionOperation`, which will drive fetching/refreshing of the local user
211
+ // based on the subscription ID we do have.
212
+ val legacyUserSyncString = _preferencesService !! .getString(PreferenceStores .ONESIGNAL , PreferenceOneSignalKeys .PREFS_LEGACY_USER_SYNCVALUES )
213
+ var suppressBackendOperation = false
214
+
215
+ if (legacyUserSyncString != null ) {
216
+ val legacyUserSyncJSON = JSONObject (legacyUserSyncString)
217
+ val notificationTypes = legacyUserSyncJSON.getInt(" notification_types" )
218
+
219
+ val pushSubscriptionModel = SubscriptionModel ()
220
+ pushSubscriptionModel.id = legacyPlayerId
221
+ pushSubscriptionModel.type = SubscriptionType .PUSH
222
+ pushSubscriptionModel.optedIn = notificationTypes != SubscriptionStatus .NO_PERMISSION .value && notificationTypes != SubscriptionStatus .UNSUBSCRIBE .value
223
+ pushSubscriptionModel.address = legacyUserSyncJSON.safeString(" identifier" ) ? : " "
224
+ pushSubscriptionModel.status = SubscriptionStatus .fromInt(notificationTypes) ? : SubscriptionStatus .NO_PERMISSION
225
+ _configModel !! .pushSubscriptionId = legacyPlayerId
226
+ _subscriptionModelStore !! .add(pushSubscriptionModel, ModelChangeTags .NO_PROPOGATE )
227
+ suppressBackendOperation = true
228
+ }
229
+
230
+ createAndSwitchToNewUser(suppressBackendOperation = suppressBackendOperation)
231
+
232
+ _operationRepo !! .enqueue(LoginUserFromSubscriptionOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId, legacyPlayerId))
233
+ _preferencesService !! .saveString(PreferenceStores .ONESIGNAL , PreferenceOneSignalKeys .PREFS_LEGACY_PLAYER_ID , null )
234
+ }
194
235
} else {
195
236
Logging .debug(" initWithContext: using cached user ${_identityModelStore !! .model.onesignalId} " )
196
237
_operationRepo !! .enqueue(RefreshUserOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId))
@@ -276,7 +317,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
276
317
}
277
318
}
278
319
279
- private fun createAndSwitchToNewUser (modify : ((identityModel: IdentityModel , propertiesModel: PropertiesModel ) -> Unit )? = null) {
320
+ private fun createAndSwitchToNewUser (suppressBackendOperation : Boolean = false, modify : ((identityModel: IdentityModel , propertiesModel: PropertiesModel ) -> Unit )? = null) {
280
321
Logging .debug(" createAndSwitchToNewUser()" )
281
322
282
323
// create a new identity and properties model locally
@@ -321,7 +362,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
321
362
_subscriptionModelStore !! .clear(ModelChangeTags .NO_PROPOGATE )
322
363
_identityModelStore !! .replace(identityModel)
323
364
_propertiesModelStore !! .replace(propertiesModel)
324
- _subscriptionModelStore !! .replaceAll(subscriptions)
365
+ _subscriptionModelStore !! .replaceAll(subscriptions, if (suppressBackendOperation) ModelChangeTags . NO_PROPOGATE else ModelChangeTags . NORMAL )
325
366
}
326
367
327
368
override fun <T > hasService (c : Class <T >): Boolean = _services .hasService(c)
0 commit comments