@@ -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
@@ -16,6 +17,9 @@ import com.onesignal.core.internal.application.impl.ApplicationService
16
17
import com.onesignal.core.internal.config.ConfigModel
17
18
import com.onesignal.core.internal.config.ConfigModelStore
18
19
import com.onesignal.core.internal.operations.IOperationRepo
20
+ import com.onesignal.core.internal.preferences.IPreferencesService
21
+ import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys
22
+ import com.onesignal.core.internal.preferences.PreferenceStores
19
23
import com.onesignal.core.internal.startup.StartupService
20
24
import com.onesignal.debug.IDebugManager
21
25
import com.onesignal.debug.LogLevel
@@ -33,6 +37,7 @@ import com.onesignal.user.UserModule
33
37
import com.onesignal.user.internal.backend.IdentityConstants
34
38
import com.onesignal.user.internal.identity.IdentityModel
35
39
import com.onesignal.user.internal.identity.IdentityModelStore
40
+ import com.onesignal.user.internal.operations.LoginUserFromSubscriptionOperation
36
41
import com.onesignal.user.internal.operations.LoginUserOperation
37
42
import com.onesignal.user.internal.operations.RefreshUserOperation
38
43
import com.onesignal.user.internal.operations.TransferSubscriptionOperation
@@ -42,6 +47,7 @@ import com.onesignal.user.internal.subscriptions.SubscriptionModel
42
47
import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
43
48
import com.onesignal.user.internal.subscriptions.SubscriptionStatus
44
49
import com.onesignal.user.internal.subscriptions.SubscriptionType
50
+ import org.json.JSONObject
45
51
46
52
internal class OneSignalImp : IOneSignal , IServiceProvider {
47
53
override val sdkVersion: String = OneSignalUtils .sdkVersion
@@ -87,6 +93,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
87
93
private var _propertiesModelStore : PropertiesModelStore ? = null
88
94
private var _subscriptionModelStore : SubscriptionModelStore ? = null
89
95
private var _startupService : StartupService ? = null
96
+ private var _preferencesService : IPreferencesService ? = null
90
97
91
98
// Other State
92
99
private val _services : ServiceProvider
@@ -182,14 +189,48 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
182
189
_propertiesModelStore = _services .getService()
183
190
_identityModelStore = _services .getService()
184
191
_subscriptionModelStore = _services .getService()
192
+ _preferencesService = _services .getService()
185
193
186
194
// Instantiate and call the IStartableServices
187
195
_startupService = _services .getService()
188
196
_startupService !! .bootstrap()
189
197
190
198
if (forceCreateUser || ! _identityModelStore !! .model.hasProperty(IdentityConstants .ONESIGNAL_ID )) {
191
- createAndSwitchToNewUser()
192
- _operationRepo !! .enqueue(LoginUserOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId, _identityModelStore !! .model.externalId))
199
+ val legacyPlayerId = _preferencesService !! .getString(PreferenceStores .ONESIGNAL , PreferenceOneSignalKeys .PREFS_LEGACY_PLAYER_ID )
200
+ if (legacyPlayerId == null ) {
201
+ Logging .debug(" initWithContext: creating new device-scoped user" )
202
+ createAndSwitchToNewUser()
203
+ _operationRepo !! .enqueue(LoginUserOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId, _identityModelStore !! .model.externalId))
204
+ }
205
+ else {
206
+ Logging .debug(" initWithContext: creating user linked to subscription $legacyPlayerId " )
207
+
208
+ // Converting a 4.x SDK to the 5.x SDK. We pull the legacy user sync values to create the subscription model, then enqueue
209
+ // a specialized `LoginUserFromSubscriptionOperation`, which will drive fetching/refreshing of the local user
210
+ // based on the subscription ID we do have.
211
+ val legacyUserSyncString = _preferencesService !! .getString(PreferenceStores .ONESIGNAL , PreferenceOneSignalKeys .PREFS_LEGACY_USER_SYNCVALUES )
212
+ var suppressBackendOperation = false
213
+
214
+ if (legacyUserSyncString != null ) {
215
+ val legacyUserSyncJSON = JSONObject (legacyUserSyncString)
216
+ val notificationTypes = legacyUserSyncJSON.getInt(" notification_types" )
217
+
218
+ val pushSubscriptionModel = SubscriptionModel ()
219
+ pushSubscriptionModel.id = legacyPlayerId
220
+ pushSubscriptionModel.type = SubscriptionType .PUSH
221
+ pushSubscriptionModel.optedIn = notificationTypes != SubscriptionStatus .NO_PERMISSION .value && notificationTypes != SubscriptionStatus .UNSUBSCRIBE .value
222
+ pushSubscriptionModel.address = legacyUserSyncJSON.safeString(" identifier" ) ? : " "
223
+ pushSubscriptionModel.status = SubscriptionStatus .fromInt(notificationTypes) ? : SubscriptionStatus .NO_PERMISSION
224
+ _configModel !! .pushSubscriptionId = legacyPlayerId
225
+ _subscriptionModelStore !! .add(pushSubscriptionModel, ModelChangeTags .NO_PROPOGATE )
226
+ suppressBackendOperation = true
227
+ }
228
+
229
+ createAndSwitchToNewUser(suppressBackendOperation = suppressBackendOperation)
230
+
231
+ _operationRepo !! .enqueue(LoginUserFromSubscriptionOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId, legacyPlayerId))
232
+ _preferencesService !! .saveString(PreferenceStores .ONESIGNAL , PreferenceOneSignalKeys .PREFS_LEGACY_PLAYER_ID , null )
233
+ }
193
234
} else {
194
235
Logging .debug(" initWithContext: using cached user ${_identityModelStore !! .model.onesignalId} " )
195
236
_operationRepo !! .enqueue(RefreshUserOperation (_configModel !! .appId, _identityModelStore !! .model.onesignalId))
@@ -299,7 +340,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
299
340
}
300
341
}
301
342
302
- private fun createAndSwitchToNewUser (modify : ((identityModel: IdentityModel , propertiesModel: PropertiesModel ) -> Unit )? = null) {
343
+ private fun createAndSwitchToNewUser (suppressBackendOperation : Boolean = false, modify : ((identityModel: IdentityModel , propertiesModel: PropertiesModel ) -> Unit )? = null) {
303
344
Logging .debug(" createAndSwitchToNewUser()" )
304
345
305
346
// create a new identity and properties model locally
@@ -345,7 +386,10 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
345
386
_identityModelStore !! .replace(identityModel)
346
387
_propertiesModelStore !! .replace(propertiesModel)
347
388
348
- if (currentPushSubscription != null ) {
389
+ if (suppressBackendOperation) {
390
+ _subscriptionModelStore !! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
391
+ }
392
+ else if (currentPushSubscription != null ) {
349
393
_operationRepo !! .enqueue(TransferSubscriptionOperation (_configModel !! .appId, currentPushSubscription.id, sdkId))
350
394
_subscriptionModelStore !! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
351
395
} else {
0 commit comments