Skip to content

Commit

Permalink
fixes to detecting permission changes
Browse files Browse the repository at this point in the history
* Simplify by passing notification types to user manager, instead of also accounting for permission observer changes and setting the `reachable` property separately.
* Notification types changes will drive other changes on the subscription model, like setting reachable, optedin, and enabled.
  • Loading branch information
nan-li committed Feb 17, 2023
1 parent c0b8da4 commit b5aee76
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ typedef void (^OSNotificationOpenedBlock)(OSNotificationOpenedResult * _Nonnull
// can check responds to selector
- (void)setNotificationTypes:(int)notificationTypes;
- (void)setPushToken:(NSString * _Nonnull)pushToken;
- (void)setReachable:(BOOL)inReachable;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,6 @@ + (void)updateNotificationTypes:(int)notificationTypes {
+ (void)sendNotificationTypesUpdateToDelegate {
// We don't delay observer update to wait until the OneSignal server is notified
// TODO: We can do the above and delay observers until server is updated.
if (self.delegate && [self.delegate respondsToSelector:@selector(setReachable:)]) {
[self.delegate setReachable:[self getNotificationTypes] > 0];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(setNotificationTypes:)]) {
[self.delegate setNotificationTypes:[self getNotificationTypes]];
}
Expand Down
5 changes: 0 additions & 5 deletions iOS_SDK/OneSignalSDK/OneSignalNotifications/OSPermission.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ + (void)fireChangesObserver:(OSPermissionStateInternal*)state {
OSNotificationsManager.lastPermissionState = [state copy];
[OSNotificationsManager.lastPermissionState persistAsFrom];
}
// Update the push subscription's _accepted property
// TODO: This can be called before the User Manager has set itself as the delegate
if (OSNotificationsManager.delegate && [OSNotificationsManager.delegate respondsToSelector:@selector(setReachable:)]) {
[OSNotificationsManager.delegate setReachable:state.reachable];
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public class OSPushSubscriptionState: NSObject {
"optedIn": optedIn
]
}

func equals(_ state: OSPushSubscriptionState) -> Bool {
return self.id == state.id && self.token == state.token && self.optedIn == state.optedIn
}
}

@objc
Expand Down Expand Up @@ -156,9 +160,10 @@ class OSSubscriptionModel: OSModel {

// If _isDisabled is set, this supersedes as the value to send to server.
if _isDisabled && notificationTypes != -2 {
notificationTypes = -2
return
}

_reachable = notificationTypes > 0
self.set(property: "notificationTypes", newValue: notificationTypes)
}
}
Expand All @@ -174,7 +179,6 @@ class OSSubscriptionModel: OSModel {
guard self.type == .push && _reachable != oldValue else {
return
}
updateNotificationTypes()
firePushSubscriptionChanged(.reachable(oldValue))
}
}
Expand All @@ -185,7 +189,7 @@ class OSSubscriptionModel: OSModel {
guard self.type == .push && _isDisabled != oldValue else {
return
}
updateNotificationTypes()
notificationTypes = -2
firePushSubscriptionChanged(.isDisabled(oldValue))
}
}
Expand Down Expand Up @@ -358,6 +362,11 @@ extension OSSubscriptionModel {

let newSubscriptionState = OSPushSubscriptionState(id: subscriptionId, token: address, optedIn: newIsOptedIn)

// TODO: Make this method less hacky, this is a final check before firing push observer
guard !prevSubscriptionState.equals(newSubscriptionState) else {
return
}

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

// TODO: Don't fire observer until server is udated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,11 +739,4 @@ extension OneSignalUserManagerImpl: OneSignalNotificationsDelegate {
}
user.pushSubscriptionModel.address = pushToken
}

public func setReachable(_ inReachable: Bool) {
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
return
}
user.pushSubscriptionModel._reachable = inReachable
}
}

0 comments on commit b5aee76

Please sign in to comment.