@@ -269,14 +269,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
269
269
)
270
270
if (legacyPlayerId == null ) {
271
271
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 )
280
273
} else {
281
274
Logging .debug(" initWithContext: creating user linked to subscription $legacyPlayerId " )
282
275
@@ -325,7 +318,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
325
318
suppressBackendOperation = true
326
319
}
327
320
328
- createAndSwitchToNewUser(suppressBackendOperation = suppressBackendOperation )
321
+ createAndSwitchToNewUser(suppressBackendOperation = true )
329
322
330
323
// ** No longer allowed when identity verification is on
331
324
operationRepo!! .enqueue(
@@ -380,7 +373,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
380
373
}
381
374
382
375
// TODO: Set JWT Token for all future requests.
383
- createAndSwitchToNewUser { identityModel, _ ->
376
+ createAndSwitchToNewUser(suppressBackendOperation = false ) { identityModel, _ ->
384
377
identityModel.externalId = externalId
385
378
identityModel.jwtToken = jwtBearerToken
386
379
}
@@ -396,17 +389,31 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
396
389
// time if network conditions prevent the operation to succeed. This allows us to
397
390
// provide a callback to the caller when we can absolutely say the user is logged
398
391
// in, so they may take action on their own backend.
392
+
399
393
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 ) {
410
417
Logging .log(LogLevel .ERROR , " Could not login user" )
411
418
}
412
419
}
@@ -455,7 +462,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
455
462
for (model in identityModelStore!! .store.list()) {
456
463
if (externalId == model.externalId) {
457
464
identityModelStore!! .model.jwtToken = token
458
- operationRepo!! .setPaused(false )
459
465
operationRepo!! .forceExecuteOperations()
460
466
Logging .log(LogLevel .DEBUG , " JWT $token is updated for externalId $externalId " )
461
467
return
@@ -494,6 +500,20 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
494
500
modify(identityModel, propertiesModel)
495
501
}
496
502
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
+ ) {
497
517
val subscriptions = mutableListOf<SubscriptionModel >()
498
518
499
519
// Create the push subscription for this device under the new user, copying the current
@@ -502,7 +522,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
502
522
// will be automatically transferred over to this new user being created. If there is no
503
523
// current push subscription we do a "normal" replace which will drive adding a CreateSubscriptionOperation
504
524
// to the queue.
505
- val currentPushSubscription = subscriptionModelStore!! .list().firstOrNull { it.id == configModel !! .pushSubscriptionId }
525
+ val currentPushSubscription = subscriptionModelStore!! .list().firstOrNull { it.type == SubscriptionType . PUSH }
506
526
val newPushSubscription = SubscriptionModel ()
507
527
508
528
newPushSubscription.id = currentPushSubscription?.id ? : IDManager .createLocalId()
@@ -530,7 +550,13 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
530
550
531
551
if (suppressBackendOperation) {
532
552
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
+ ) {
534
560
operationRepo!! .enqueue(TransferSubscriptionOperation (configModel!! .appId, currentPushSubscription.id, sdkId))
535
561
subscriptionModelStore!! .replaceAll(subscriptions, ModelChangeTags .NO_PROPOGATE )
536
562
} else {
@@ -539,14 +565,18 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
539
565
}
540
566
541
567
private fun resumeOperationRepoAfterFetchParams (configModel : ConfigModel ) {
542
- // pause operation repo until useIdentityVerification is determined
543
- operationRepo!! .setPaused(true )
544
568
configModel.addFetchParamsObserver(
545
569
object : FetchParamsObserver {
546
570
override fun onParamsFetched (params : ParamsObject ) {
547
571
// resume operations if identity verification is turned off or a jwt is cached
548
572
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
+ )
550
580
} else {
551
581
Logging .log(LogLevel .ERROR , " A valid JWT is required for user ${identityModelStore!! .model.externalId} ." )
552
582
}
0 commit comments