@@ -261,14 +261,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
261261 )
262262 if (legacyPlayerId == null ) {
263263 Logging .debug(" initWithContext: creating new device-scoped user" )
264- createAndSwitchToNewUser()
265- operationRepo!! .enqueue(
266- LoginUserOperation (
267- configModel!! .appId,
268- identityModelStore!! .model.onesignalId,
269- identityModelStore!! .model.externalId,
270- ),
271- )
264+ createAndSwitchToNewUser(suppressBackendOperation = true )
272265 } else {
273266 Logging .debug(" initWithContext: creating user linked to subscription $legacyPlayerId " )
274267
@@ -317,7 +310,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
317310 suppressBackendOperation = true
318311 }
319312
320- createAndSwitchToNewUser(suppressBackendOperation = suppressBackendOperation )
313+ createAndSwitchToNewUser(suppressBackendOperation = true )
321314
322315 // ** No longer allowed when identity verification is on
323316 operationRepo!! .enqueue(
@@ -372,7 +365,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
372365 }
373366
374367 // TODO: Set JWT Token for all future requests.
375- createAndSwitchToNewUser { identityModel, _ ->
368+ createAndSwitchToNewUser(suppressBackendOperation = false ) { identityModel, _ ->
376369 identityModel.externalId = externalId
377370 identityModel.jwtToken = jwtBearerToken
378371 }
@@ -388,17 +381,31 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
388381 // time if network conditions prevent the operation to succeed. This allows us to
389382 // provide a callback to the caller when we can absolutely say the user is logged
390383 // in, so they may take action on their own backend.
384+
391385 val result =
392- operationRepo!! .enqueueAndWait(
393- LoginUserOperation (
394- configModel!! .appId,
395- newIdentityOneSignalId,
396- externalId,
397- if (currentIdentityExternalId == null ) currentIdentityOneSignalId else null ,
398- ),
399- )
400-
401- if (! result) {
386+ when (useIdentityVerification) {
387+ true -> {
388+ operationRepo!! .enqueue(
389+ LoginUserOperation (
390+ configModel!! .appId,
391+ identityModelStore!! .model.onesignalId,
392+ identityModelStore!! .model.externalId,
393+ ),
394+ )
395+ }
396+ else -> {
397+ operationRepo!! .enqueueAndWait(
398+ LoginUserOperation (
399+ configModel!! .appId,
400+ newIdentityOneSignalId,
401+ externalId,
402+ if (currentIdentityExternalId == null ) currentIdentityOneSignalId else null ,
403+ ),
404+ )
405+ }
406+ }
407+
408+ if (result == false ) {
402409 Logging .log(LogLevel .ERROR , " Could not login user" )
403410 }
404411 }
@@ -437,7 +444,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
437444 for (model in identityModelStore!! .store.list()) {
438445 if (externalId == model.externalId) {
439446 identityModelStore!! .model.jwtToken = token
440- operationRepo!! .setPaused(false )
441447 operationRepo!! .forceExecuteOperations()
442448 Logging .log(LogLevel .DEBUG , " JWT $token is updated for externalId $externalId " )
443449 return
@@ -476,6 +482,20 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
476482 modify(identityModel, propertiesModel)
477483 }
478484
485+ if (! identityModel.jwtToken.isNullOrEmpty()) {
486+ setupNewSubscription(identityModel, propertiesModel, suppressBackendOperation, sdkId)
487+ }
488+
489+ identityModelStore!! .replace(identityModel)
490+ propertiesModelStore!! .replace(propertiesModel)
491+ }
492+
493+ private fun setupNewSubscription (
494+ identityModel : IdentityModel ,
495+ propertiesModel : PropertiesModel ,
496+ suppressBackendOperation : Boolean ,
497+ sdkId : String ,
498+ ) {
479499 val subscriptions = mutableListOf<SubscriptionModel >()
480500
481501 // Create the push subscription for this device under the new user, copying the current
@@ -484,7 +504,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
484504 // will be automatically transferred over to this new user being created. If there is no
485505 // current push subscription we do a "normal" replace which will drive adding a CreateSubscriptionOperation
486506 // to the queue.
487- val currentPushSubscription = subscriptionModelStore!! .list().firstOrNull { it.id == configModel !! .pushSubscriptionId }
507+ val currentPushSubscription = subscriptionModelStore!! .list().firstOrNull { it.type == SubscriptionType . PUSH }
488508 val newPushSubscription = SubscriptionModel ()
489509
490510 newPushSubscription.id = currentPushSubscription?.id ? : IDManager .createLocalId()
@@ -512,7 +532,13 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
512532
513533 if (suppressBackendOperation) {
514534 subscriptionModelStore!! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
515- } else if (currentPushSubscription != null ) {
535+ } else if (currentPushSubscription != null && (
536+ ! useIdentityVerification || useIdentityVerification &&
537+ ! IDManager .isLocalId(
538+ currentPushSubscription.id,
539+ )
540+ )
541+ ) {
516542 operationRepo!! .enqueue(TransferSubscriptionOperation (configModel!! .appId, currentPushSubscription.id, sdkId))
517543 subscriptionModelStore!! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
518544 } else {
@@ -521,14 +547,18 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
521547 }
522548
523549 private fun resumeOperationRepoAfterFetchParams (configModel : ConfigModel ) {
524- // pause operation repo until useIdentityVerification is determined
525- operationRepo!! .setPaused(true )
526550 configModel.addFetchParamsObserver(
527551 object : FetchParamsObserver {
528552 override fun onParamsFetched (params : ParamsObject ) {
529553 // resume operations if identity verification is turned off or a jwt is cached
530554 if (params.useIdentityVerification == false || identityModelStore!! .model.jwtToken != null ) {
531- operationRepo!! .setPaused(false )
555+ operationRepo!! .enqueue(
556+ LoginUserOperation (
557+ configModel!! .appId,
558+ identityModelStore!! .model.onesignalId,
559+ identityModelStore!! .model.externalId,
560+ ),
561+ )
532562 } else {
533563 Logging .log(LogLevel .ERROR , " A valid JWT is required for user ${identityModelStore!! .model.externalId} ." )
534564 }
0 commit comments