Skip to content

Commit 8689a9e

Browse files
committed
fix push sub hydration logic (still faulty)
* Motivation: Hydrating the push subscription is problematic. We will hydrate it if: - the push subscription ID does not already exist locally - the type of the returned subscription is iOSPush - the token at the time the request is made matches the returned token - we hydrate using the first matching subscription that is returned (there may be multiple iOS push subscriptions in the response but we will accept the first one that fulfills our conditions above) * This can still be faulty behavior but we get as close as we can to correct behavior for now.
1 parent ffebc25 commit 8689a9e

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSUserRequests.swift

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,31 @@ class OSUserExecutor {
6363
}
6464

6565
/**
66-
Used to parse Create User and Fetch User responses.
66+
Used to parse Create User and Fetch User responses. The `originalPushToken` is the push token when the request was created, which may be different from the push token currently in the SDK (How again can that happen?). This is used to determine whether or not to hydrate the push subscription.
6767
*/
68-
static func parseFetchUserResponse(response: [AnyHashable:Any], identityModel: OSIdentityModel) {
68+
static func parseFetchUserResponse(response: [AnyHashable:Any], identityModel: OSIdentityModel, originalPushToken: String?) {
6969
// On success, check if the current user is the same as the one in the request
7070
// If user has changed, don't hydrate, except for push subscription
7171
let modelInStore = OneSignalUserManagerImpl.sharedInstance.identityModelStore.getModel(key: OS_IDENTITY_MODEL_KEY)
7272

73-
// TODO: Determine if we should be hydrating the push sub if sub_id exists in the SDK. I think not, revisit.
74-
// Only hydrate the push subscription ID if it does not exist in the SDK
75-
if (OneSignalUserManagerImpl.sharedInstance.user.pushSubscriptionModel.subscriptionId == nil) {
76-
if let subscriptionObject = parseSubscriptionObjectResponse(response) {
77-
for subModel in subscriptionObject {
78-
if let subType = subModel["type"] as? String {
79-
if subType == "iOSPush" {
80-
OneSignalUserManagerImpl.sharedInstance.user.pushSubscriptionModel.hydrate(subModel)
81-
if let subId = subModel["id"] as? String {
82-
OSNotificationsManager.setPushSubscriptionId(subId)
83-
}
84-
}
73+
// TODO: Determine how to hydrate the push subscription, which is still faulty.
74+
// Hydrate by token if sub_id exists?
75+
// Problem: a user can have multiple iOS push subscription, and perhaps missing token
76+
// Ideally we only get push subscription for this device in the response, not others
77+
78+
// Hydrate the push subscription if we don't already have a subscription ID AND token matches the original request
79+
if (OneSignalUserManagerImpl.sharedInstance.user.pushSubscriptionModel.subscriptionId == nil),
80+
let subscriptionObject = parseSubscriptionObjectResponse(response)
81+
{
82+
for subModel in subscriptionObject {
83+
if subModel["type"] as? String == "iOSPush",
84+
subModel["token"] as? String == originalPushToken
85+
{
86+
OneSignalUserManagerImpl.sharedInstance.user.pushSubscriptionModel.hydrate(subModel)
87+
if let subId = subModel["id"] as? String {
88+
OSNotificationsManager.setPushSubscriptionId(subId)
8589
}
90+
break;
8691
}
8792
}
8893
}
@@ -104,8 +109,9 @@ class OSUserExecutor {
104109
let models = OneSignalUserManagerImpl.sharedInstance.subscriptionModelStore.getModels()
105110
for subModel in subscriptionObject {
106111
if let address = subModel["token"] as? String,
107-
let type = OSSubscriptionType(rawValue: subModel["type"] as? String ?? ""),
108-
subModel["type"] as? String != "iOSPush"
112+
let rawType = subModel["type"] as? String,
113+
rawType != "iOSPush",
114+
let type = OSSubscriptionType(rawValue: rawType)
109115
{
110116
if let model = models[address] {
111117
// This subscription exists in the store, hydrate
@@ -115,7 +121,7 @@ class OSUserExecutor {
115121
// This subscription does not exist in the store, add
116122
OneSignalUserManagerImpl.sharedInstance.subscriptionModelStore.add(id: address, model: OSSubscriptionModel(
117123
type: type,
118-
address: subModel["token"] as? String,
124+
address: address,
119125
subscriptionId: subModel["id"] as? String,
120126
accepted: true,
121127
isDisabled: false,
@@ -141,6 +147,7 @@ class OSUserExecutor {
141147

142148
// We will pass minimal properties to this request
143149
static func createUser(_ user: OSUserInternal) {
150+
let originalPushToken = user.pushSubscriptionModel.address
144151
let request = OSRequestCreateUser(identityModel: user.identityModel, pushSubscriptionModel: user.pushSubscriptionModel)
145152

146153
// Currently there are no requirements needed before sending this request
@@ -149,7 +156,7 @@ class OSUserExecutor {
149156
}
150157
OneSignalClient.shared().execute(request) { response in
151158
if let response = response {
152-
parseFetchUserResponse(response: response, identityModel: request.identityModel)
159+
parseFetchUserResponse(response: response, identityModel: request.identityModel, originalPushToken: originalPushToken)
153160
}
154161
executePendingRequests()
155162
} onFailure: { error in
@@ -237,7 +244,7 @@ class OSUserExecutor {
237244

238245
OneSignalClient.shared().execute(request) { response in
239246
if let response = response {
240-
parseFetchUserResponse(response: response, identityModel: request.identityModel)
247+
parseFetchUserResponse(response: response, identityModel: request.identityModel, originalPushToken: OneSignalUserManagerImpl.sharedInstance.token)
241248
}
242249
} onFailure: { _ in
243250
// What?

0 commit comments

Comments
 (0)