Skip to content

Commit 0dc2cee

Browse files
committed
Disable push sub when logout called (JWT on)
1 parent 7d5db5e commit 0dc2cee

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Modeling/OSSubscriptionModel.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ enum OSSubscriptionType: String {
9898
Internal subscription model.
9999
*/
100100
class OSSubscriptionModel: OSModel {
101+
struct Constants {
102+
static let isDisabledInternallyKey = "isDisabledInternallyKey"
103+
}
104+
101105
var type: OSSubscriptionType
102106

103107
var address: String? { // This is token on push subs so must remain Optional
@@ -194,6 +198,21 @@ class OSSubscriptionModel: OSModel {
194198
}
195199
}
196200

201+
/**
202+
Set to `true` by the SDK when logout is called with Identity Verification turned on.
203+
The properties of `_isDisabled` and `notificationTypes` remain unchanged, to maintain correct data.
204+
When a subscription update is made, this value will be read and `enabled = false` and `notification_types = -2` will be sent.
205+
When a user logs in, this property will be set to `false` and the subscription will be included in the User Create request..
206+
*/
207+
var _isDisabledInternally = false {
208+
didSet {
209+
guard _isDisabledInternally != oldValue else {
210+
return
211+
}
212+
self.set(property: Constants.isDisabledInternallyKey, newValue: _isDisabledInternally)
213+
}
214+
}
215+
197216
// Properties for push subscription
198217
var testType: Int? {
199218
didSet {

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Modeling/OSSubscriptionModelStoreListener.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ class OSSubscriptionModelStoreListener: OSModelStoreListener {
6262
}
6363

6464
func getUpdateModelDelta(_ args: OSModelChangedArgs) -> OSDelta? {
65+
/*
66+
Don't generate a Delta if setting internal disable to false, which will generate a subscription update.
67+
This means a user is logging in and a create user will be sent with the updated subscription included.
68+
*/
69+
if args.property == OSSubscriptionModel.Constants.isDisabledInternallyKey && args.newValue as? Bool == false {
70+
return nil
71+
}
72+
6573
return OSDelta(
6674
name: OS_UPDATE_SUBSCRIPTION_DELTA,
6775
identityModelId: OneSignalUserManagerImpl.sharedInstance.user.identityModel.modelId,

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
400400
}
401401
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignalUserManager internal _login called with externalId: \(externalId ?? "nil")")
402402

403+
if externalId != nil {
404+
pushSubscriptionModel?._isDisabledInternally = false
405+
}
406+
403407
/*
404408
Logging in to a "new-to-the-sdk" externalId from an anonymous user, if JWT is OFF or UNKNOWN.
405409

@@ -440,6 +444,13 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
440444
prepareForNewUser()
441445
_user = nil
442446
createUserIfNil()
447+
448+
/*
449+
If Identity Verification is on, disable the push subscription.
450+
*/
451+
if jwtConfig.isRequired == true {
452+
user.pushSubscriptionModel._isDisabledInternally = true
453+
}
443454
}
444455

445456
@objc

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSRequestUpdateSubscription.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,23 @@ class OSRequestUpdateSubscription: OneSignalRequest, OSUserRequest {
6565
var subscriptionParams = subscriptionObject
6666
subscriptionParams.removeValue(forKey: "address")
6767
subscriptionParams.removeValue(forKey: "notificationTypes")
68+
subscriptionParams.removeValue(forKey: OSSubscriptionModel.Constants.isDisabledInternallyKey)
6869
subscriptionParams["token"] = subscriptionModel.address
6970
subscriptionParams["device_os"] = subscriptionModel.deviceOs
7071
subscriptionParams["sdk"] = subscriptionModel.sdk
7172
subscriptionParams["app_version"] = subscriptionModel.appVersion
7273

73-
// notificationTypes defaults to -1 instead of nil, don't send if it's -1
74-
if subscriptionModel.notificationTypes != -1 {
75-
subscriptionParams["notification_types"] = subscriptionModel.notificationTypes
74+
if subscriptionModel._isDisabledInternally {
75+
subscriptionParams["enabled"] = false
76+
subscriptionParams["notification_types"] = -2
77+
} else {
78+
// notificationTypes defaults to -1 instead of nil, don't send if it's -1
79+
if subscriptionModel.notificationTypes != -1 {
80+
subscriptionParams["notification_types"] = subscriptionModel.notificationTypes
81+
}
82+
subscriptionParams["enabled"] = subscriptionModel.enabled
7683
}
7784

78-
subscriptionParams["enabled"] = subscriptionModel.enabled
7985
// TODO: The above is not quite right. If we hydrate, we will over-write any pending updates
8086
// May use subscriptionObject, but enabled and notification_types should be sent together...
8187

0 commit comments

Comments
 (0)