@@ -504,8 +504,8 @@ class OSUserExecutor {
504504 }
505505 }
506506
507- static func fetchUser( aliasLabel: String , aliasId: String , identityModel: OSIdentityModel ) {
508- let request = OSRequestFetchUser ( identityModel: identityModel, aliasLabel: aliasLabel, aliasId: aliasId)
507+ static func fetchUser( aliasLabel: String , aliasId: String , identityModel: OSIdentityModel , onNewSession : Bool = false ) {
508+ let request = OSRequestFetchUser ( identityModel: identityModel, aliasLabel: aliasLabel, aliasId: aliasId, onNewSession : onNewSession )
509509
510510 appendToQueue ( request)
511511
@@ -528,6 +528,28 @@ class OSUserExecutor {
528528 // Clear local data in preparation for hydration
529529 OneSignalUserManagerImpl . sharedInstance. clearUserData ( )
530530 parseFetchUserResponse ( response: response, identityModel: request. identityModel, originalPushToken: OneSignalUserManagerImpl . sharedInstance. token)
531+
532+ // If this is a on-new-session's fetch user call, check that the subscription still exists
533+ if request. onNewSession,
534+ OneSignalUserManagerImpl . sharedInstance. isCurrentUser ( request. identityModel) ,
535+ let subId = OneSignalUserManagerImpl . sharedInstance. user. pushSubscriptionModel. subscriptionId,
536+ let subscriptionObjects = parseSubscriptionObjectResponse ( response)
537+ {
538+ var subscriptionExists = false
539+ for subModel in subscriptionObjects {
540+ if subModel [ " id " ] as? String == subId {
541+ subscriptionExists = true
542+ break
543+ }
544+ }
545+
546+ if !subscriptionExists {
547+ // This subscription probably has been deleted
548+ OneSignalLog . onesignalLog ( . LL_ERROR, message: " OSUserExecutor.executeFetchUserRequest found this device's push subscription gone, now send the push subscription to server. " )
549+ OneSignalUserManagerImpl . sharedInstance. pushSubscriptionModel? . subscriptionId = nil
550+ OneSignalUserManagerImpl . sharedInstance. createPushSubscriptionRequest ( )
551+ }
552+ }
531553 }
532554 executePendingRequests ( )
533555 } onFailure: { error in
@@ -594,23 +616,7 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
594616 // When reading from the cache, update the push subscription model
595617 func updatePushSubscriptionModel( _ pushSubscriptionModel: OSSubscriptionModel ) {
596618 self . pushSubscriptionModel = pushSubscriptionModel
597- // Push Subscription Object
598- var pushSubscriptionObject : [ String : Any ] = [ : ]
599- pushSubscriptionObject [ " id " ] = pushSubscriptionModel. subscriptionId
600- pushSubscriptionObject [ " type " ] = pushSubscriptionModel. type. rawValue
601- pushSubscriptionObject [ " token " ] = pushSubscriptionModel. address
602- pushSubscriptionObject [ " enabled " ] = pushSubscriptionModel. enabled
603- pushSubscriptionObject [ " test_type " ] = pushSubscriptionModel. testType
604- pushSubscriptionObject [ " device_os " ] = pushSubscriptionModel. deviceOs
605- pushSubscriptionObject [ " sdk " ] = pushSubscriptionModel. sdk
606- pushSubscriptionObject [ " device_model " ] = pushSubscriptionModel. deviceModel
607- pushSubscriptionObject [ " app_version " ] = pushSubscriptionModel. appVersion
608- pushSubscriptionObject [ " net_type " ] = pushSubscriptionModel. netType
609- // notificationTypes defaults to -1 instead of nil, don't send if it's -1
610- if pushSubscriptionModel. notificationTypes != - 1 {
611- pushSubscriptionObject [ " notification_types " ] = pushSubscriptionModel. notificationTypes
612- }
613- self . parameters ? [ " subscriptions " ] = [ pushSubscriptionObject]
619+ self . parameters ? [ " subscriptions " ] = [ pushSubscriptionModel. jsonRepresentation ( ) ]
614620 }
615621
616622 init ( identityModel: OSIdentityModel , propertiesModel: OSPropertiesModel , pushSubscriptionModel: OSSubscriptionModel , originalPushToken: String ? ) {
@@ -836,7 +842,8 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
836842 let identityModel : OSIdentityModel
837843 let aliasLabel : String
838844 let aliasId : String
839-
845+ let onNewSession : Bool
846+
840847 func prepareForExecution( ) -> Bool {
841848 guard let appId = OneSignalConfigManager . getAppId ( ) else {
842849 OneSignalLog . onesignalLog ( . LL_DEBUG, message: " Cannot generate the fetch user request due to null app ID. " )
@@ -847,10 +854,11 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
847854 return true
848855 }
849856
850- init ( identityModel: OSIdentityModel , aliasLabel: String , aliasId: String ) {
857+ init ( identityModel: OSIdentityModel , aliasLabel: String , aliasId: String , onNewSession : Bool ) {
851858 self . identityModel = identityModel
852859 self . aliasLabel = aliasLabel
853860 self . aliasId = aliasId
861+ self . onNewSession = onNewSession
854862 self . stringDescription = " OSRequestFetchUser with aliasLabel: \( aliasLabel) aliasId: \( aliasId) "
855863 super. init ( )
856864 self . method = GET
@@ -861,6 +869,7 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
861869 coder. encode ( aliasLabel, forKey: " aliasLabel " )
862870 coder. encode ( aliasId, forKey: " aliasId " )
863871 coder. encode ( identityModel, forKey: " identityModel " )
872+ coder. encode ( onNewSession, forKey: " onNewSession " )
864873 coder. encode ( method. rawValue, forKey: " method " ) // Encodes as String
865874 coder. encode ( timestamp, forKey: " timestamp " )
866875 }
@@ -879,6 +888,7 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
879888 self . identityModel = identityModel
880889 self . aliasLabel = aliasLabel
881890 self . aliasId = aliasId
891+ self . onNewSession = coder. decodeBool ( forKey: " onNewSession " )
882892 self . stringDescription = " OSRequestFetchUser with aliasLabel: \( aliasLabel) aliasId: \( aliasId) "
883893 super. init ( )
884894 self . method = HTTPMethod ( rawValue: rawMethod)
@@ -1107,8 +1117,9 @@ class OSRequestUpdateProperties: OneSignalRequest, OSUserRequest {
11071117}
11081118
11091119/**
1110- Current uses of this request are for adding Email and SMS subscriptions. Push subscriptions won't be created using
1111- this request because they will be created with ``OSRequestCreateUser``.
1120+ Primary uses of this request are for adding Email and SMS subscriptions. Push subscriptions typically won't be created using
1121+ this request because they will be created with ``OSRequestCreateUser``. However, if we detect that this device's
1122+ push subscription is ever deleted, we will make a request to create it again.
11121123 */
11131124class OSRequestCreateSubscription : OneSignalRequest , OSUserRequest {
11141125 var sentToClient = false
@@ -1137,15 +1148,7 @@ class OSRequestCreateSubscription: OneSignalRequest, OSUserRequest {
11371148 self . identityModel = identityModel
11381149 self . stringDescription = " OSRequestCreateSubscription with subscriptionModel: \( subscriptionModel. address ?? " nil " ) "
11391150 super. init ( )
1140-
1141- var subscriptionParams : [ String : Any ] = [ : ]
1142- subscriptionParams [ " type " ] = subscriptionModel. type. rawValue
1143- subscriptionParams [ " token " ] = subscriptionModel. address
1144- // 1/5/2023: For email and SMS, either send `enabled` AND `notification_types` or don't send either
1145- // TODO: ^ Backend changes may require us to come back and change this request's payload.
1146- // TODO: Since this is not used for push, don't send either of those. Revisit if we ever create push subscriptions with this request.
1147-
1148- self . parameters = [ " subscription " : subscriptionParams]
1151+ self . parameters = [ " subscription " : subscriptionModel. jsonRepresentation ( ) ]
11491152 self . method = POST
11501153 _ = prepareForExecution ( ) // sets the path property
11511154 }
0 commit comments