Skip to content

Commit e8f197e

Browse files
nan-lijinliu9508
authored andcommitted
Don't hydrate push subscription status
Existing Problem: - If on a previous session, `optOut()` was called, we will hydrate the push subscription with the status of `UNSUBSCRIBE(-2)` on the next session. - However, we lose the true device-level subscription status in doing so. - Then, when `optIn()` is called, we were using `UNSUBSCRIBE(-2)` along with `optedIn` to calculate the data to send in the Update Subscription request. - This resulted in incorrect values being sent and reports of `optIn()` method not working. Solution: - We can still hydrate the push subscription model's `optedIn` property if we see either UNSUBSCRIBE(-2) or DISABLED_FROM_REST_API_DEFAULT_REASON(-30). - However, when the subscription model store is replacing all of the subscriptions in the refresh user operation, keep the existing push subscription `status`. - This way, when `optIn()` is called again, we can use device-level status with the `optedIn` boolean to calculate values to send to the server.
1 parent a7f27ec commit e8f197e

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/RefreshUserOperationExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ internal class RefreshUserOperationExecutor(
105105
SubscriptionType.PUSH
106106
}
107107
}
108-
subscriptionModel.optedIn = subscriptionModel.status != SubscriptionStatus.UNSUBSCRIBE
108+
subscriptionModel.optedIn = subscriptionModel.status != SubscriptionStatus.UNSUBSCRIBE && subscriptionModel.status != SubscriptionStatus.DISABLED_FROM_REST_API_DEFAULT_REASON
109109
subscriptionModel.sdk = subscription.sdk ?: ""
110110
subscriptionModel.deviceOS = subscription.deviceOS ?: ""
111111
subscriptionModel.carrier = subscription.carrier ?: ""

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/subscriptions/SubscriptionModel.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ enum class SubscriptionStatus(val value: Int) {
6767
/** The subscription is not enabled due to an FCM authentication failed IOException */
6868
FIREBASE_FCM_ERROR_IOEXCEPTION_AUTHENTICATION_FAILED(-29),
6969

70+
/** The subscription is not enabled because the app has disabled the subscription via API */
71+
DISABLED_FROM_REST_API_DEFAULT_REASON(-30),
72+
7073
/** The subscription is not enabled due to some other (unknown locally) error */
7174
ERROR(9999),
7275
;
@@ -79,6 +82,10 @@ enum class SubscriptionStatus(val value: Int) {
7982
}
8083

8184
class SubscriptionModel : Model() {
85+
/**
86+
* Reflects user preference only, defaults true.
87+
* The public API for [IPushSubscription.optedIn] considers this value AND permission.
88+
*/
8289
var optedIn: Boolean
8390
get() = getBooleanProperty(::optedIn.name)
8491
set(value) {
@@ -97,6 +104,13 @@ class SubscriptionModel : Model() {
97104
setStringProperty(::address.name, value)
98105
}
99106

107+
/**
108+
* This reflects the "device-level" subscription status.
109+
*
110+
* For example, if [IPushSubscription.optOut] is called, the SDK sends UNSUBSCRIBE(-2) to the server.
111+
* However, locally on the model, we still keep the existing status.
112+
* It is necessary when [IPushSubscription.optIn] is called again to know the true device status.
113+
*/
100114
var status: SubscriptionStatus
101115
get() {
102116
if (!hasProperty(::status.name)) {

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/subscriptions/SubscriptionModelStore.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ open class SubscriptionModelStore(prefs: IPreferencesService) : SimpleModelStore
2424
model.deviceOS = existingPushModel.deviceOS
2525
model.carrier = existingPushModel.carrier
2626
model.appVersion = existingPushModel.appVersion
27+
model.status = existingPushModel.status
2728
}
2829
break
2930
}

0 commit comments

Comments
 (0)