Skip to content

Commit 29314f2

Browse files
authored
Merge pull request #2327 from OneSignal/subscription-status-not-updated
fix: logout incorrectly uses the old subscription ID
2 parents 8f470c0 + f208562 commit 29314f2

File tree

1 file changed

+20
-13
lines changed
  • OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal

1 file changed

+20
-13
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,10 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
467467
// to the queue.
468468
val currentPushSubscription = subscriptionModelStore!!.list().firstOrNull { it.id == configModel!!.pushSubscriptionId }
469469
val newPushSubscription = SubscriptionModel()
470+
val localSubscriptionID = IDManager.createLocalId()
471+
val currentSubscriptionID = currentPushSubscription?.id ?: localSubscriptionID
470472

471-
newPushSubscription.id = currentPushSubscription?.id ?: IDManager.createLocalId()
473+
newPushSubscription.id = currentSubscriptionID
472474
newPushSubscription.type = SubscriptionType.PUSH
473475
newPushSubscription.optedIn = currentPushSubscription?.optedIn ?: true
474476
newPushSubscription.address = currentPushSubscription?.address ?: ""
@@ -478,11 +480,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
478480
newPushSubscription.carrier = DeviceUtils.getCarrierName(services.getService<IApplicationService>().appContext) ?: ""
479481
newPushSubscription.appVersion = AndroidUtils.getAppVersion(services.getService<IApplicationService>().appContext) ?: ""
480482

481-
// ensure we always know this devices push subscription ID
482-
configModel!!.pushSubscriptionId = newPushSubscription.id
483-
484-
subscriptions.add(newPushSubscription)
485-
486483
// The next 4 lines makes this user the effective user locally. We clear the subscriptions
487484
// first as a `NO_PROPOGATE` change because we don't want to drive deleting the cleared subscriptions
488485
// 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 {
491488
identityModelStore!!.replace(identityModel)
492489
propertiesModelStore!!.replace(propertiesModel)
493490

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+
}
501502
}
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)
502509
}
503510

504511
override fun <T> hasService(c: Class<T>): Boolean = services.hasService(c)

0 commit comments

Comments
 (0)