Skip to content

Commit 988f113

Browse files
committed
push sub optIn and optOut
* Add optedIn property, and funcs optIn() and optOut() * We still keep internal properties like isDisabled and calculate enabled for sending to backend, and for the observer. * OSPushSubscriptionState given to observers will still have: id, token, enabled * Also use `pushSubscriptionId` instead of going through the protocol `pushSubscription.subscriptionId`
1 parent b4968a2 commit 988f113

File tree

5 files changed

+62
-52
lines changed

5 files changed

+62
-52
lines changed

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSSubscriptionModel.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@ import OneSignalNotifications
3838

3939
@objc
4040
public class OSPushSubscriptionState: NSObject {
41-
@objc public let subscriptionId: String?
41+
// TODO: Decide if optedIn will be observed
42+
@objc public let id: String?
4243
@objc public let token: String?
4344
@objc public let enabled: Bool
4445

45-
init(subscriptionId: String?, token: String?, enabled: Bool) {
46-
self.subscriptionId = subscriptionId
46+
init(id: String?, token: String?, enabled: Bool) {
47+
self.id = id
4748
self.token = token
4849
self.enabled = enabled
4950
}
5051

5152
func toDictionary() -> NSDictionary {
52-
let subscriptionId = self.subscriptionId ?? ""
53-
let token = self.token ?? ""
53+
let id = self.id ?? "nil"
54+
let token = self.token ?? "nil"
5455
return [
55-
"subscriptionId": subscriptionId,
56+
"id": id,
5657
"token": token,
5758
"enabled": enabled
5859
]
@@ -132,7 +133,14 @@ class OSSubscriptionModel: OSModel {
132133
return calculateIsSubscribed(subscriptionId: subscriptionId, address: address, accepted: _accepted, isDisabled: _isDisabled)
133134
}
134135
}
135-
136+
137+
var optedIn: Bool {
138+
// optedIn = permission + userPreference
139+
get {
140+
return _accepted && !_isDisabled
141+
}
142+
}
143+
136144
// Push Subscription Only
137145
// Initialize to be -1, so not to deal with unwrapping every time
138146
var notificationTypes = -1 {
@@ -162,7 +170,7 @@ class OSSubscriptionModel: OSModel {
162170
}
163171
}
164172

165-
// Set by the app developer when they set User.pushSubscription.enabled
173+
// Set by the app developer when they call User.pushSubscription.optOut()
166174
var _isDisabled: Bool { // Default to false for all subscriptions
167175
didSet {
168176
guard self.type == .push && _isDisabled != oldValue else {
@@ -246,7 +254,7 @@ class OSSubscriptionModel: OSModel {
246254
extension OSSubscriptionModel {
247255
// Only used for the push subscription model
248256
var currentPushSubscriptionState: OSPushSubscriptionState {
249-
return OSPushSubscriptionState(subscriptionId: self.subscriptionId,
257+
return OSPushSubscriptionState(id: self.subscriptionId,
250258
token: self.address,
251259
enabled: self.enabled
252260
)
@@ -277,28 +285,28 @@ extension OSSubscriptionModel {
277285
func firePushSubscriptionChanged(_ changedProperty: OSPushPropertyChanged) {
278286
var prevIsSubscribed = true
279287
var prevIsEnabled = true
280-
var prevSubscriptionState = OSPushSubscriptionState(subscriptionId: "", token: "", enabled: true)
288+
var prevSubscriptionState = OSPushSubscriptionState(id: "", token: "", enabled: true)
281289

282290
switch changedProperty {
283291
case .subscriptionId(let oldValue):
284292
prevIsEnabled = calculateIsEnabled(address: address, accepted: _accepted, isDisabled: _isDisabled)
285293
prevIsSubscribed = calculateIsSubscribed(subscriptionId: oldValue, address: address, accepted: _accepted, isDisabled: _isDisabled)
286-
prevSubscriptionState = OSPushSubscriptionState(subscriptionId: oldValue, token: address, enabled: prevIsSubscribed)
294+
prevSubscriptionState = OSPushSubscriptionState(id: oldValue, token: address, enabled: prevIsSubscribed)
287295

288296
case .accepted(let oldValue):
289297
prevIsEnabled = calculateIsEnabled(address: address, accepted: oldValue, isDisabled: _isDisabled)
290298
prevIsSubscribed = calculateIsSubscribed(subscriptionId: subscriptionId, address: address, accepted: oldValue, isDisabled: _isDisabled)
291-
prevSubscriptionState = OSPushSubscriptionState(subscriptionId: subscriptionId, token: address, enabled: prevIsSubscribed)
299+
prevSubscriptionState = OSPushSubscriptionState(id: subscriptionId, token: address, enabled: prevIsSubscribed)
292300

293301
case .isDisabled(let oldValue):
294302
prevIsEnabled = calculateIsEnabled(address: address, accepted: _accepted, isDisabled: oldValue)
295303
prevIsSubscribed = calculateIsSubscribed(subscriptionId: subscriptionId, address: address, accepted: _accepted, isDisabled: oldValue)
296-
prevSubscriptionState = OSPushSubscriptionState(subscriptionId: subscriptionId, token: address, enabled: prevIsSubscribed)
304+
prevSubscriptionState = OSPushSubscriptionState(id: subscriptionId, token: address, enabled: prevIsSubscribed)
297305

298306
case .address(let oldValue):
299307
prevIsEnabled = calculateIsEnabled(address: oldValue, accepted: _accepted, isDisabled: _isDisabled)
300308
prevIsSubscribed = calculateIsSubscribed(subscriptionId: subscriptionId, address: oldValue, accepted: _accepted, isDisabled: _isDisabled)
301-
prevSubscriptionState = OSPushSubscriptionState(subscriptionId: subscriptionId, token: oldValue, enabled: prevIsSubscribed)
309+
prevSubscriptionState = OSPushSubscriptionState(id: subscriptionId, token: oldValue, enabled: prevIsSubscribed)
302310
}
303311

304312
let newIsSubscribed = calculateIsSubscribed(subscriptionId: subscriptionId, address: address, accepted: _accepted, isDisabled: _isDisabled)
@@ -309,7 +317,7 @@ extension OSSubscriptionModel {
309317
self.set(property: "enabled", newValue: newIsEnabled)
310318
}
311319

312-
let newSubscriptionState = OSPushSubscriptionState(subscriptionId: subscriptionId, token: address, enabled: newIsSubscribed)
320+
let newSubscriptionState = OSPushSubscriptionState(id: subscriptionId, token: address, enabled: newIsSubscribed)
313321

314322
let stateChanges = OSPushSubscriptionStateChanges(to: newSubscriptionState, from: prevSubscriptionState)
315323

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ import OneSignalNotifications
7979
This is the push subscription interface exposed to the public.
8080
*/
8181
@objc public protocol OSPushSubscription {
82-
var subscriptionId: String? { get }
82+
var id: String? { get }
8383
var token: String? { get }
84-
var enabled: Bool { get }
85-
func enable(_ enable: Bool) -> Bool
84+
var optedIn: Bool { get }
85+
86+
func optIn()
87+
func optOut()
8688
func addObserver(_ observer: OSPushSubscriptionObserver) -> OSPushSubscriptionState?
8789
func removeObserver(_ observer: OSPushSubscriptionObserver)
8890
}
@@ -617,7 +619,7 @@ extension OneSignalUserManagerImpl: OSUser {
617619
extension OneSignalUserManagerImpl: OSPushSubscription {
618620

619621
public func addObserver(_ observer: OSPushSubscriptionObserver) -> OSPushSubscriptionState? {
620-
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "addObserver") else {
622+
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "pushSubscription.addObserver") else {
621623
return nil
622624
}
623625
self.pushSubscriptionStateChangesObserver.addObserver(observer)
@@ -628,43 +630,43 @@ extension OneSignalUserManagerImpl: OSPushSubscription {
628630
self.pushSubscriptionStateChangesObserver.removeObserver(observer)
629631
}
630632

631-
public var subscriptionId: String? {
632-
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "subscriptionId") else {
633+
public var id: String? {
634+
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "pushSubscription.id") else {
633635
return nil
634636
}
635637
return user.pushSubscriptionModel.subscriptionId
636638
}
637639

638640
public var token: String? {
639-
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "token") else {
641+
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "pushSubscription.token") else {
640642
return nil
641643
}
642644
return user.pushSubscriptionModel.address
643645
}
644-
645-
/**
646-
Get the `enabled` state.
647-
*/
648-
public var enabled: Bool {
649-
get {
650-
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "enabled") else {
651-
return false
652-
}
653-
return user.pushSubscriptionModel.enabled
646+
647+
public var optedIn: Bool {
648+
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "pushSubscription.optedIn") else {
649+
return false
654650
}
651+
return user.pushSubscriptionModel.optedIn
655652
}
656653

657654
/**
658-
Set the `enabled` state. After being set, we return whether this was successful, as one can attempt to set `enabled` to `true` but push is not actually enabled on the device. This can be due to system level permissions or missing push token, etc.
659-
660-
- Returns: A boolean indicating if this method was successful.
655+
Enable the push subscription, and prompts if needed. `optedIn` can still be `false` after `optIn()` is called if permission is not granted.
661656
*/
662-
public func enable(_ enable: Bool) -> Bool {
663-
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "enable") else {
664-
return false
657+
public func optIn() {
658+
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "pushSubscription.optIn") else {
659+
return
660+
}
661+
user.pushSubscriptionModel._isDisabled = false
662+
OSNotificationsManager.requestPermission(nil, fallbackToSettings: true)
663+
}
664+
665+
public func optOut() {
666+
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "pushSubscription.optOut") else {
667+
return
665668
}
666-
user.pushSubscriptionModel._isDisabled = !enable
667-
return user.pushSubscriptionModel.enabled != enable
669+
user.pushSubscriptionModel._isDisabled = true
668670
}
669671
}
670672

iOS_SDK/OneSignalSDK/Source/OSMessagingController.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ - (void)messageViewPageImpressionRequest:(OSInAppMessageInternal *)message withP
427427
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"Page Impression Request page id: %@",pageId]];
428428
// Create the request and attach a payload to it
429429
let metricsRequest = [OSRequestInAppMessagePageViewed withAppId:OneSignal.appId
430-
withPlayerId:OneSignalUserManagerImpl.sharedInstance.pushSubscription.subscriptionId
430+
withPlayerId:OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId
431431
withMessageId:message.messageId
432432
withPageId:pageId
433433
forVariantId:message.variantId];
@@ -467,7 +467,7 @@ - (void)messageViewImpressionRequest:(OSInAppMessageInternal *)message {
467467

468468
// Create the request and attach a payload to it
469469
let metricsRequest = [OSRequestInAppMessageViewed withAppId:OneSignal.appId
470-
withPlayerId:OneSignalUserManagerImpl.sharedInstance.pushSubscription.subscriptionId
470+
withPlayerId:OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId
471471
withMessageId:message.messageId
472472
forVariantId:message.variantId];
473473

@@ -572,7 +572,7 @@ - (BOOL)shouldShowInAppMessage:(OSInAppMessageInternal *)message {
572572
return ![self.seenInAppMessages containsObject:message.messageId] &&
573573
[self.triggerController messageMatchesTriggers:message] &&
574574
![message isFinished] &&
575-
OneSignalUserManagerImpl.sharedInstance.pushSubscription.subscriptionId != nil;
575+
OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId != nil;
576576
return true;
577577
}
578578

@@ -853,7 +853,7 @@ - (void)sendClickRESTCall:(OSInAppMessageInternal *)message withAction:(OSInAppM
853853
[message addClickId:clickId];
854854

855855
let metricsRequest = [OSRequestInAppMessageClicked withAppId:OneSignal.appId
856-
withPlayerId:OneSignalUserManagerImpl.sharedInstance.pushSubscription.subscriptionId
856+
withPlayerId:OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId
857857
withMessageId:message.messageId
858858
forVariantId:message.variantId
859859
withAction:action];
@@ -968,15 +968,15 @@ - (void)onApplicationDidBecomeActive {
968968

969969
#pragma mark OSPushSubscriptionObserver Methods
970970
- (void)onOSPushSubscriptionChangedWithStateChanges:(OSPushSubscriptionStateChanges * _Nonnull)stateChanges {
971-
if (stateChanges.to.subscriptionId == nil) {
971+
if (stateChanges.to.id == nil) {
972972
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"onOSPushSubscriptionChangedWithStateChanges: changed to nil subscription id"];
973973
return;
974974
}
975975
// Pull new IAMs when the subscription id changes to a new valid subscription id
976-
if (stateChanges.from.subscriptionId != nil &&
977-
[stateChanges.to.subscriptionId isEqualToString:stateChanges.from.subscriptionId]) {
976+
if (stateChanges.from.id != nil &&
977+
[stateChanges.to.id isEqualToString:stateChanges.from.id]) {
978978
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"onOSPushSubscriptionChangedWithStateChanges: changed to new valid subscription id"];
979-
[self getInAppMessagesFromServer:stateChanges.to.subscriptionId];
979+
[self getInAppMessagesFromServer:stateChanges.to.id];
980980
}
981981
}
982982

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ + (void)startNewSessionInternal {
428428

429429
// This is almost always going to be nil the first time.
430430
// The OSMessagingController is an OSPushSubscriptionObserver so that we pull IAMs once we have the sub id
431-
NSString *subscriptionId = OneSignalUserManagerImpl.sharedInstance.pushSubscription.subscriptionId;
431+
NSString *subscriptionId = OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId;
432432
if (subscriptionId) {
433433
[OSMessagingController.sharedInstance getInAppMessagesFromServer:subscriptionId];
434434
}
@@ -647,7 +647,7 @@ + (void)downloadIOSParamsWithAppId:(NSString *)appId {
647647
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Downloading iOS parameters for this application"];
648648
_didCallDownloadParameters = true;
649649
// This will be nil unless we have a cached user
650-
NSString *userId = OneSignalUserManagerImpl.sharedInstance.User.pushSubscription.subscriptionId;
650+
NSString *userId = OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId;
651651
[OneSignalClient.sharedClient executeRequest:[OSRequestGetIosParams withUserId:userId appId:appId] onSuccess:^(NSDictionary *result) {
652652

653653
if (result[IOS_REQUIRES_USER_ID_AUTHENTICATION]) {
@@ -755,7 +755,7 @@ + (BOOL)sendSessionEndOutcomes:(NSNumber*)totalTimeActive params:(OSFocusCallPar
755755
}
756756

757757
NSString* onesignalId = OneSignalUserManagerImpl.sharedInstance.onesignalId;
758-
NSString* pushSubscriptionId = OneSignalUserManagerImpl.sharedInstance.pushSubscription.subscriptionId;
758+
NSString* pushSubscriptionId = OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId;
759759

760760
if (!onesignalId || !pushSubscriptionId) {
761761
return false;

iOS_SDK/OneSignalSDK/Source/OneSignalLocation.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ + (void)sendLocation {
428428
return;
429429

430430
@synchronized(OneSignalLocation.mutexObjectForLastLocation) {
431-
NSString *userId = OneSignalUserManagerImpl.sharedInstance.pushSubscription.subscriptionId;
431+
NSString *userId = OneSignalUserManagerImpl.sharedInstance.pushSubscriptionId;
432432
if (!lastLocation || !userId)
433433
return;
434434

0 commit comments

Comments
 (0)