Skip to content

Commit

Permalink
Merge pull request #1216 from OneSignal/5.0.0/fill_out_subscription_o…
Browse files Browse the repository at this point in the history
…bject

[5.0.0] Fill out subscription object and make subscription/permission fixes
  • Loading branch information
nan-li authored Jan 26, 2023
2 parents 78e9f05 + d45a79b commit afedcb4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ + (BOOL)permission {
}

+ (BOOL)canRequestPermission {
return !self.currentPermissionState.hasPrompted;
return !self.currentPermissionState.answeredPrompt;
}

+ (void)requestPermission:(OSUserResponseBlock)block {
Expand Down Expand Up @@ -423,14 +423,14 @@ + (void)setSubscriptionErrorStatus:(int)errorType {

// onOSPermissionChanged should only fire if something changed.
+ (void)addPermissionObserver:(NSObject<OSPermissionObserver>*)observer {
[_permissionStateChangesObserver addObserver:observer];
[self.permissionStateChangesObserver addObserver:observer];

if ([self.currentPermissionState compare:self.lastPermissionState])
[OSPermissionChangedInternalObserver fireChangesObserver:self.currentPermissionState];
}

+ (void)removePermissionObserver:(NSObject<OSPermissionObserver>*)observer {
[_permissionStateChangesObserver removeObserver:observer];
[self.permissionStateChangesObserver removeObserver:observer];
}

// User just responed to the iOS native notification permission prompt.
Expand Down
2 changes: 1 addition & 1 deletion iOS_SDK/OneSignalSDK/OneSignalNotifications/OSPermission.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ + (void)fireChangesObserver:(OSPermissionStateInternal*)state {
// 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(setAccepted:)]) {
[OSNotificationsManager.delegate setAccepted:state.accepted];
[OSNotificationsManager.delegate setAccepted:state.reachable];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,13 @@ class OSSubscriptionModel: OSModel {
self.set(property: "notificationTypes", newValue: notificationTypes)
}
}

// swiftlint:disable identifier_name
// This is set by the permission state changing
// Defaults to true for email & SMS, defaults to false for push
/**
This is set by the permission state changing.
Defaults to true for email & SMS, defaults to false for push.
Note that this property reflects the `reachable` property of a permission state. As provisional permission is considered to be `optedIn` and `enabled`.
*/
var _accepted: Bool {
didSet {
guard self.type == .push && _accepted != oldValue else {
Expand All @@ -191,6 +195,8 @@ class OSSubscriptionModel: OSModel {
let deviceOs = UIDevice.current.systemVersion
let sdk = ONESIGNAL_VERSION
let deviceModel: String? = OSDeviceUtils.getDeviceVariant()
let appVersion: String? = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
let netType: Int? = OSNetworkingUtils.getNetType() as? Int

// When a Subscription is initialized, it may not have a subscriptionId until a request to the backend is made.
init(type: OSSubscriptionType,
Expand Down Expand Up @@ -231,6 +237,7 @@ class OSSubscriptionModel: OSModel {
coder.encode(_accepted, forKey: "_accepted")
coder.encode(_isDisabled, forKey: "_isDisabled")
coder.encode(notificationTypes, forKey: "notificationTypes")
coder.encode(testType, forKey: "testType")
}

required init?(coder: NSCoder) {
Expand All @@ -247,6 +254,7 @@ class OSSubscriptionModel: OSModel {
self._accepted = coder.decodeBool(forKey: "_accepted")
self._isDisabled = coder.decodeBool(forKey: "_isDisabled")
self.notificationTypes = coder.decodeInteger(forKey: "notificationTypes")
self.testType = coder.decodeObject(forKey: "testType") as? Int
super.init(coder: coder)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
pushSubscriptionObject["type"] = pushSubscriptionModel.type.rawValue
pushSubscriptionObject["token"] = pushSubscriptionModel.address
pushSubscriptionObject["enabled"] = pushSubscriptionModel.enabled

pushSubscriptionObject["test_type"] = pushSubscriptionModel.testType
pushSubscriptionObject["device_os"] = pushSubscriptionModel.deviceOs
pushSubscriptionObject["sdk"] = pushSubscriptionModel.sdk
pushSubscriptionObject["device_model"] = pushSubscriptionModel.deviceModel

pushSubscriptionObject["app_version"] = pushSubscriptionModel.appVersion
pushSubscriptionObject["net_type"] = pushSubscriptionModel.netType
// notificationTypes defaults to -1 instead of nil, don't send if it's -1
if pushSubscriptionModel.notificationTypes != -1 {
pushSubscriptionObject["notification_types"] = pushSubscriptionModel.notificationTypes
Expand Down

0 comments on commit afedcb4

Please sign in to comment.