@@ -269,14 +269,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
269269 )
270270 if (legacyPlayerId == null ) {
271271 Logging .debug(" initWithContext: creating new device-scoped user" )
272- createAndSwitchToNewUser()
273- operationRepo!! .enqueue(
274- LoginUserOperation (
275- configModel!! .appId,
276- identityModelStore!! .model.onesignalId,
277- identityModelStore!! .model.externalId,
278- ),
279- )
272+ createAndSwitchToNewUser(suppressBackendOperation = true )
280273 } else {
281274 Logging .debug(" initWithContext: creating user linked to subscription $legacyPlayerId " )
282275
@@ -325,7 +318,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
325318 suppressBackendOperation = true
326319 }
327320
328- createAndSwitchToNewUser(suppressBackendOperation = suppressBackendOperation )
321+ createAndSwitchToNewUser(suppressBackendOperation = true )
329322
330323 // ** No longer allowed when identity verification is on
331324 operationRepo!! .enqueue(
@@ -380,7 +373,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
380373 }
381374
382375 // TODO: Set JWT Token for all future requests.
383- createAndSwitchToNewUser { identityModel, _ ->
376+ createAndSwitchToNewUser(suppressBackendOperation = false ) { identityModel, _ ->
384377 identityModel.externalId = externalId
385378 identityModel.jwtToken = jwtBearerToken
386379 }
@@ -396,17 +389,31 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
396389 // time if network conditions prevent the operation to succeed. This allows us to
397390 // provide a callback to the caller when we can absolutely say the user is logged
398391 // in, so they may take action on their own backend.
392+
399393 val result =
400- operationRepo!! .enqueueAndWait(
401- LoginUserOperation (
402- configModel!! .appId,
403- newIdentityOneSignalId,
404- externalId,
405- if (currentIdentityExternalId == null ) currentIdentityOneSignalId else null ,
406- ),
407- )
408-
409- if (! result) {
394+ when (useIdentityVerification) {
395+ true -> {
396+ operationRepo!! .enqueue(
397+ LoginUserOperation (
398+ configModel!! .appId,
399+ identityModelStore!! .model.onesignalId,
400+ identityModelStore!! .model.externalId,
401+ ),
402+ )
403+ }
404+ else -> {
405+ operationRepo!! .enqueueAndWait(
406+ LoginUserOperation (
407+ configModel!! .appId,
408+ newIdentityOneSignalId,
409+ externalId,
410+ if (currentIdentityExternalId == null ) currentIdentityOneSignalId else null ,
411+ ),
412+ )
413+ }
414+ }
415+
416+ if (result == false ) {
410417 Logging .log(LogLevel .ERROR , " Could not login user" )
411418 }
412419 }
@@ -455,7 +462,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
455462 for (model in identityModelStore!! .store.list()) {
456463 if (externalId == model.externalId) {
457464 identityModelStore!! .model.jwtToken = token
458- operationRepo!! .setPaused(false )
459465 operationRepo!! .forceExecuteOperations()
460466 Logging .log(LogLevel .DEBUG , " JWT $token is updated for externalId $externalId " )
461467 return
@@ -494,6 +500,20 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
494500 modify(identityModel, propertiesModel)
495501 }
496502
503+ if (! identityModel.jwtToken.isNullOrEmpty()) {
504+ setupNewSubscription(identityModel, propertiesModel, suppressBackendOperation, sdkId)
505+ }
506+
507+ identityModelStore!! .replace(identityModel)
508+ propertiesModelStore!! .replace(propertiesModel)
509+ }
510+
511+ private fun setupNewSubscription (
512+ identityModel : IdentityModel ,
513+ propertiesModel : PropertiesModel ,
514+ suppressBackendOperation : Boolean ,
515+ sdkId : String ,
516+ ) {
497517 val subscriptions = mutableListOf<SubscriptionModel >()
498518
499519 // Create the push subscription for this device under the new user, copying the current
@@ -502,7 +522,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
502522 // will be automatically transferred over to this new user being created. If there is no
503523 // current push subscription we do a "normal" replace which will drive adding a CreateSubscriptionOperation
504524 // to the queue.
505- val currentPushSubscription = subscriptionModelStore!! .list().firstOrNull { it.id == configModel !! .pushSubscriptionId }
525+ val currentPushSubscription = subscriptionModelStore!! .list().firstOrNull { it.type == SubscriptionType . PUSH }
506526 val newPushSubscription = SubscriptionModel ()
507527
508528 newPushSubscription.id = currentPushSubscription?.id ? : IDManager .createLocalId()
@@ -530,7 +550,13 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
530550
531551 if (suppressBackendOperation) {
532552 subscriptionModelStore!! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
533- } else if (currentPushSubscription != null ) {
553+ } else if (currentPushSubscription != null && (
554+ ! useIdentityVerification || useIdentityVerification &&
555+ ! IDManager .isLocalId(
556+ currentPushSubscription.id,
557+ )
558+ )
559+ ) {
534560 operationRepo!! .enqueue(TransferSubscriptionOperation (configModel!! .appId, currentPushSubscription.id, sdkId))
535561 subscriptionModelStore!! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
536562 } else {
@@ -539,14 +565,18 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
539565 }
540566
541567 private fun resumeOperationRepoAfterFetchParams (configModel : ConfigModel ) {
542- // pause operation repo until useIdentityVerification is determined
543- operationRepo!! .setPaused(true )
544568 configModel.addFetchParamsObserver(
545569 object : FetchParamsObserver {
546570 override fun onParamsFetched (params : ParamsObject ) {
547571 // resume operations if identity verification is turned off or a jwt is cached
548572 if (params.useIdentityVerification == false || identityModelStore!! .model.jwtToken != null ) {
549- operationRepo!! .setPaused(false )
573+ operationRepo!! .enqueue(
574+ LoginUserOperation (
575+ configModel!! .appId,
576+ identityModelStore!! .model.onesignalId,
577+ identityModelStore!! .model.externalId,
578+ ),
579+ )
550580 } else {
551581 Logging .log(LogLevel .ERROR , " A valid JWT is required for user ${identityModelStore!! .model.externalId} ." )
552582 }
0 commit comments