@@ -467,8 +467,10 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
467
467
// to the queue.
468
468
val currentPushSubscription = subscriptionModelStore!! .list().firstOrNull { it.id == configModel!! .pushSubscriptionId }
469
469
val newPushSubscription = SubscriptionModel ()
470
+ val localSubscriptionID = IDManager .createLocalId()
471
+ val currentSubscriptionID = currentPushSubscription?.id ? : localSubscriptionID
470
472
471
- newPushSubscription.id = currentPushSubscription?.id ? : IDManager .createLocalId()
473
+ newPushSubscription.id = currentSubscriptionID
472
474
newPushSubscription.type = SubscriptionType .PUSH
473
475
newPushSubscription.optedIn = currentPushSubscription?.optedIn ? : true
474
476
newPushSubscription.address = currentPushSubscription?.address ? : " "
@@ -478,11 +480,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
478
480
newPushSubscription.carrier = DeviceUtils .getCarrierName(services.getService<IApplicationService >().appContext) ? : " "
479
481
newPushSubscription.appVersion = AndroidUtils .getAppVersion(services.getService<IApplicationService >().appContext) ? : " "
480
482
481
- // ensure we always know this devices push subscription ID
482
- configModel!! .pushSubscriptionId = newPushSubscription.id
483
-
484
- subscriptions.add(newPushSubscription)
485
-
486
483
// The next 4 lines makes this user the effective user locally. We clear the subscriptions
487
484
// first as a `NO_PROPOGATE` change because we don't want to drive deleting the cleared subscriptions
488
485
// on the backend. Once cleared we can then setup the new identity/properties model, and add
@@ -491,14 +488,24 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
491
488
identityModelStore!! .replace(identityModel)
492
489
propertiesModelStore!! .replace(propertiesModel)
493
490
494
- if (suppressBackendOperation) {
495
- subscriptionModelStore!! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
496
- } else if (currentPushSubscription != null ) {
497
- operationRepo!! .enqueue(TransferSubscriptionOperation (configModel!! .appId, currentPushSubscription.id, sdkId))
498
- subscriptionModelStore!! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
499
- } else {
500
- subscriptionModelStore!! .replaceAll(subscriptions)
491
+ var changeTag = ModelChangeTags .NO_PROPOGATE
492
+
493
+ if (! suppressBackendOperation) {
494
+ if (currentPushSubscription?.id != null && identityModel.externalId != null ) {
495
+ // add a transfer-subscription operation when switching user
496
+ operationRepo!! .enqueue(TransferSubscriptionOperation (configModel!! .appId, currentPushSubscription.id, sdkId))
497
+ } else {
498
+ // reset subscription when calling logout or login for the first time
499
+ newPushSubscription.id = localSubscriptionID
500
+ changeTag = ModelChangeTags .NORMAL
501
+ }
501
502
}
503
+
504
+ // ensure we always know this devices push subscription ID
505
+ configModel!! .pushSubscriptionId = newPushSubscription.id
506
+
507
+ subscriptions.add(newPushSubscription)
508
+ subscriptionModelStore!! .replaceAll(subscriptions, changeTag)
502
509
}
503
510
504
511
override fun <T > hasService (c : Class <T >): Boolean = services.hasService(c)
0 commit comments