Skip to content

Commit 60e30a5

Browse files
committed
fix: respect REST API-disabled push subscriptions
A push subscription disabled through the REST API (notification_types -31) was re-enabled by the SDK: RefreshUser discarded the server's disable state for push, the session-start self-heal re-asserted local truth over it, and every subscription payload recomputed enabled from device state. Mirror the server's disable code on the push model when RefreshUser reports it, report it back in subscription payloads instead of the device-derived values, skip the stuck-subscription self-heal for it, and carry it across the login/logout user switch. The mirror clears when the server reports any other state and on an explicit optIn(). The 404 recovery paths (user rebuild and update-404 re-create) treat the dead record's disable as gone and recreate from device truth. Also remove the mislabeled DISABLED_FROM_REST_API_DEFAULT_REASON(-30) enum case; no OneSignal API has ever written -30 as a REST disable. Enum-name persistence now parses leniently in the shared model accessor, so models and queued operations persisted under an unknown enum name read as SUBSCRIBED instead of throwing on upgrade.
1 parent 18bc069 commit 60e30a5

14 files changed

Lines changed: 362 additions & 26 deletions

File tree

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/modeling/Model.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ open class Model(
544544
val value = getOptAnyProperty(name) ?: return null
545545

546546
if (value is T) return value
547-
if (value is String) return enumValueOf<T>(value)
547+
// Enum properties persist by name; a name this build's enum lacks (a removed case, or a
548+
// downgrade from a newer SDK) must read as null rather than throw at model load.
549+
if (value is String) return enumValues<T>().firstOrNull { it.name == value }
548550
return value as T
549551
}
550552

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/PushSubscription.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ internal open class PushSubscription(
2222
get() = model.optedIn && model.status != SubscriptionStatus.NO_PERMISSION
2323

2424
override fun optIn() {
25+
// A deliberate opt-in overrides a REST API disable; clearing it with a NORMAL-tagged
26+
// change drives a subscription update that re-enables it on the server.
27+
if (model.restApiDisabledReason != 0) {
28+
model.restApiDisabledReason = 0
29+
}
2530
// we set `optedIn` using the lower level method so we can set `forceChange=true`, which
2631
// will result in *always* driving change notification.
2732
model.setBooleanProperty(SubscriptionModel::optedIn.name, true, forceChange = true)

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserSwitcher.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class UserSwitcher(
6868
optedIn = currentPushSubscription?.optedIn ?: true
6969
address = currentPushSubscription?.address ?: ""
7070
status = currentPushSubscription?.status ?: SubscriptionStatus.NO_PERMISSION
71+
restApiDisabledReason = currentPushSubscription?.restApiDisabledReason ?: 0
7172
sdk = oneSignalUtils.sdkVersion
7273
deviceOS = this@UserSwitcher.deviceOS ?: ""
7374
carrier = carrierName ?: ""

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/builduser/impl/RebuildUserService.kt‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.user.internal.builduser.impl
22

3+
import com.onesignal.common.modeling.ModelChangeTags
34
import com.onesignal.core.internal.config.ConfigModelStore
45
import com.onesignal.core.internal.operations.Operation
56
import com.onesignal.user.internal.builduser.IRebuildUserService
@@ -8,6 +9,7 @@ import com.onesignal.user.internal.identity.IdentityModelStore
89
import com.onesignal.user.internal.operations.CreateSubscriptionOperation
910
import com.onesignal.user.internal.operations.LoginUserOperation
1011
import com.onesignal.user.internal.operations.RefreshUserOperation
12+
import com.onesignal.user.internal.operations.impl.listeners.SubscriptionModelStoreListener
1113
import com.onesignal.user.internal.properties.PropertiesModel
1214
import com.onesignal.user.internal.properties.PropertiesModelStore
1315
import com.onesignal.user.internal.subscriptions.SubscriptionModel
@@ -48,20 +50,36 @@ class RebuildUserService(
4850
operations.add(LoginUserOperation(appId, onesignalId, identityModel.externalId))
4951
val pushSubscription = subscriptionModels.firstOrNull { it.id == _configModelStore.model.pushSubscriptionId }
5052
if (pushSubscription != null) {
51-
operations.add(
52-
CreateSubscriptionOperation(
53-
appId,
54-
onesignalId,
55-
identityModel.externalId,
56-
pushSubscription.id,
57-
pushSubscription.type,
58-
pushSubscription.optedIn,
59-
pushSubscription.address,
60-
pushSubscription.status,
61-
),
62-
)
53+
operations.add(buildPushRecoveryOperation(appId, onesignalId, identityModel.externalId, pushSubscription))
6354
}
6455
operations.add(RefreshUserOperation(appId, onesignalId, identityModel.externalId))
6556
return operations
6657
}
58+
59+
// The server records this rebuild recreates no longer exist, so a recorded REST API
60+
// disable died with them; clear it and recreate from device truth.
61+
private fun buildPushRecoveryOperation(
62+
appId: String,
63+
onesignalId: String,
64+
externalId: String?,
65+
pushSubscription: SubscriptionModel,
66+
): CreateSubscriptionOperation {
67+
_subscriptionsModelStore.get(pushSubscription.id)?.setIntProperty(
68+
SubscriptionModel::restApiDisabledReason.name,
69+
0,
70+
ModelChangeTags.HYDRATE,
71+
)
72+
pushSubscription.restApiDisabledReason = 0
73+
val (enabled, status) = SubscriptionModelStoreListener.getSubscriptionEnabledAndStatus(pushSubscription)
74+
return CreateSubscriptionOperation(
75+
appId,
76+
onesignalId,
77+
externalId,
78+
pushSubscription.id,
79+
pushSubscription.type,
80+
enabled,
81+
pushSubscription.address,
82+
status,
83+
)
84+
}
6785
}

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/CreateSubscriptionOperation.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class CreateSubscriptionOperation() : Operation(SubscriptionOperationExecutor.CR
7777
* The status of this subscription.
7878
*/
7979
var status: SubscriptionStatus
80-
get() = getEnumProperty(::status.name)
80+
// A persisted name this build's enum lacks reads as SUBSCRIBED instead of dropping the op batch.
81+
get() = getOptEnumProperty<SubscriptionStatus>(::status.name) ?: SubscriptionStatus.SUBSCRIBED
8182
private set(value) {
8283
setEnumProperty(::status.name, value)
8384
}

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/UpdateSubscriptionOperation.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class UpdateSubscriptionOperation() : Operation(SubscriptionOperationExecutor.UP
7676
* The status of this subscription.
7777
*/
7878
var status: SubscriptionStatus
79-
get() = getEnumProperty(::status.name)
79+
// A persisted name this build's enum lacks reads as SUBSCRIBED instead of dropping the op batch.
80+
get() = getOptEnumProperty<SubscriptionStatus>(::status.name) ?: SubscriptionStatus.SUBSCRIBED
8081
private set(value) {
8182
setEnumProperty(::status.name, value)
8283
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ internal class RefreshUserOperationExecutor(
125125
SubscriptionType.PUSH
126126
}
127127
}
128-
subscriptionModel.optedIn = subscriptionModel.status != SubscriptionStatus.UNSUBSCRIBE && subscriptionModel.status != SubscriptionStatus.DISABLED_FROM_REST_API_DEFAULT_REASON
128+
subscriptionModel.optedIn = subscriptionModel.status != SubscriptionStatus.UNSUBSCRIBE &&
129+
subscriptionModel.status != SubscriptionStatus.DISABLED_FROM_REST_API
129130
subscriptionModel.sdk = subscription.sdk ?: ""
130131
subscriptionModel.deviceOS = subscription.deviceOS ?: ""
131132
subscriptionModel.carrier = subscription.carrier ?: ""
@@ -136,6 +137,7 @@ internal class RefreshUserOperationExecutor(
136137
if (subscriptionModel.type != SubscriptionType.PUSH) {
137138
subscriptionModels.add(subscriptionModel)
138139
} else if (subscription.id == pushSubscriptionIdFromConfig && pushSelfHealOperationForStuckSubscription == null) {
140+
hydrateRestApiDisableState(subscription, pushSubscriptionIdFromConfig)
139141
// Self-heal for users stuck at "Never Subscribed". Older SDK builds dispatched
140142
// the merged create-subscription + update-subscription(SUBSCRIBED) batch as a
141143
// POST /subscriptions carrying the already-existing server-side id; the server
@@ -218,7 +220,10 @@ internal class RefreshUserOperationExecutor(
218220

219221
val (localEnabled, localStatus) = SubscriptionModelStoreListener.getSubscriptionEnabledAndStatus(cachedPushSubscriptionModel)
220222
val serverEnabled = (serverSubscription.enabled == true) && ((serverSubscription.notificationTypes ?: 0) > 0)
221-
val divergent = localEnabled && !serverEnabled
223+
// A REST API disable is deliberate suppression, not the stuck-subscription drift this
224+
// self-heal exists for; leave it in place.
225+
val serverDisabledViaRestApi = SubscriptionStatus.isRestApiDisable(serverSubscription.notificationTypes)
226+
val divergent = localEnabled && !serverEnabled && !serverDisabledViaRestApi
222227

223228
return if (divergent) {
224229
Logging.info(
@@ -242,6 +247,28 @@ internal class RefreshUserOperationExecutor(
242247
}
243248
}
244249

250+
/**
251+
* Records or clears the server's REST API disable state on the cached push model. Only that
252+
* state is server-owned; the device stays the source of truth for the rest of the push model,
253+
* which is why push subscriptions are otherwise not hydrated from the backend.
254+
*/
255+
private fun hydrateRestApiDisableState(
256+
serverSubscription: SubscriptionObject,
257+
pushSubscriptionId: String,
258+
) {
259+
val cachedPushSubscriptionModel = _subscriptionsModelStore.get(pushSubscriptionId) ?: return
260+
val serverTypes = serverSubscription.notificationTypes ?: return
261+
// The recorded reason mirrors the server's field: -31 records, any other reported value clears.
262+
val target = if (SubscriptionStatus.isRestApiDisable(serverTypes)) serverTypes else 0
263+
if (cachedPushSubscriptionModel.restApiDisabledReason != target) {
264+
cachedPushSubscriptionModel.setIntProperty(
265+
SubscriptionModel::restApiDisabledReason.name,
266+
target,
267+
ModelChangeTags.HYDRATE,
268+
)
269+
}
270+
}
271+
245272
companion object {
246273
const val REFRESH_USER = "refresh-user"
247274
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import com.onesignal.user.internal.operations.CreateSubscriptionOperation
3131
import com.onesignal.user.internal.operations.DeleteSubscriptionOperation
3232
import com.onesignal.user.internal.operations.TransferSubscriptionOperation
3333
import com.onesignal.user.internal.operations.UpdateSubscriptionOperation
34+
import com.onesignal.user.internal.operations.impl.listeners.SubscriptionModelStoreListener
3435
import com.onesignal.user.internal.operations.impl.states.NewRecordsState
3536
import com.onesignal.user.internal.subscriptions.SubscriptionModel
3637
import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
@@ -265,11 +266,22 @@ internal class SubscriptionOperationExecutor(
265266
// emitting Creates with the same subscriptionId, so they dedupe instead
266267
// of producing two POST /users subscription rows. HYDRATE prevents the
267268
// SubscriptionModelStoreListener from enqueuing follow-on operations.
268-
_subscriptionModelStore.get(staleSubscriptionId)?.setStringProperty(
269+
val recoveryModel = _subscriptionModelStore.get(staleSubscriptionId)
270+
recoveryModel?.setStringProperty(
269271
SubscriptionModel::id.name,
270272
recoveryLocalId,
271273
ModelChangeTags.HYDRATE,
272274
)
275+
// The stale record died with any recorded REST API disable; recreate from
276+
// device truth rather than the values frozen on the failed operation.
277+
recoveryModel?.setIntProperty(
278+
SubscriptionModel::restApiDisabledReason.name,
279+
0,
280+
ModelChangeTags.HYDRATE,
281+
)
282+
val (recoveryEnabled, recoveryStatus) =
283+
recoveryModel?.let { SubscriptionModelStoreListener.getSubscriptionEnabledAndStatus(it) }
284+
?: Pair(lastOperation.enabled, lastOperation.status)
273285
if (_configModelStore.model.pushSubscriptionId == staleSubscriptionId) {
274286
_configModelStore.model.pushSubscriptionId = recoveryLocalId
275287
}
@@ -284,9 +296,9 @@ internal class SubscriptionOperationExecutor(
284296
lastOperation.externalId,
285297
recoveryLocalId,
286298
lastOperation.type,
287-
lastOperation.enabled,
299+
recoveryEnabled,
288300
lastOperation.address,
289-
lastOperation.status,
301+
recoveryStatus,
290302
),
291303
),
292304
)

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/listeners/SubscriptionModelStoreListener.kt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ internal class SubscriptionModelStoreListener(
7373
val status: SubscriptionStatus
7474
val enabled: Boolean
7575

76-
if (model.optedIn && model.status == SubscriptionStatus.SUBSCRIBED && model.address.isNotEmpty()) {
76+
// A REST API disable is server-owned; report it back rather than the device state so
77+
// subscription payloads don't re-enable a suppressed subscription.
78+
if (SubscriptionStatus.isRestApiDisable(model.restApiDisabledReason)) {
79+
enabled = false
80+
status = SubscriptionStatus.DISABLED_FROM_REST_API
81+
} else if (model.optedIn && model.status == SubscriptionStatus.SUBSCRIBED && model.address.isNotEmpty()) {
7782
enabled = true
7883
status = SubscriptionStatus.SUBSCRIBED
7984
} else {

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ enum class SubscriptionStatus(val value: Int) {
6767
/** The subscription is not enabled due to an FCM authentication failed IOException, this can be retried */
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),
70+
/** The subscription is not enabled because it was disabled through the REST API */
71+
DISABLED_FROM_REST_API(-31),
7272

7373
/** The subscription is not enabled due to some other (unknown locally) error */
7474
ERROR(9999),
@@ -101,6 +101,15 @@ enum class SubscriptionStatus(val value: Int) {
101101
FIREBASE_FCM_ERROR_IOEXCEPTION_AUTHENTICATION_FAILED, // -29
102102
)
103103

104+
/**
105+
* True when [value] is the code the server uses for a subscription disabled through the
106+
* REST API, which is only -31. The SDK never derives it from device state, and
107+
* server-reported error codes stay device-recoverable.
108+
*/
109+
fun isRestApiDisable(value: Int?): Boolean {
110+
return value == DISABLED_FROM_REST_API.value
111+
}
112+
104113
fun fromInt(value: Int): SubscriptionStatus? {
105114
return SubscriptionStatus.values().firstOrNull { it.value == value }
106115
}
@@ -135,6 +144,19 @@ class SubscriptionModel : Model() {
135144
setBooleanProperty(::isDisabledInternally.name, value)
136145
}
137146

147+
/**
148+
* The server's REST API disable code (-31), or 0 when the server has not disabled this
149+
* subscription. Hydrated by RefreshUser and never derived from device state; while set,
150+
* [SubscriptionModelStoreListener] reports `enabled = false` with this status so subscription
151+
* payloads don't re-enable a suppressed subscription. Cleared when the server reports any
152+
* other state, or by [IPushSubscription.optIn].
153+
*/
154+
var restApiDisabledReason: Int
155+
get() = getIntProperty(::restApiDisabledReason.name) { 0 }
156+
set(value) {
157+
setIntProperty(::restApiDisabledReason.name, value)
158+
}
159+
138160
var type: SubscriptionType
139161
get() = getEnumProperty(::type.name)
140162
set(value) {
@@ -160,7 +182,8 @@ class SubscriptionModel : Model() {
160182
setEnumProperty(::status.name, SubscriptionStatus.SUBSCRIBED)
161183
}
162184

163-
return getEnumProperty(::status.name)
185+
// A persisted name this build's enum lacks reads as SUBSCRIBED instead of throwing.
186+
return getOptEnumProperty<SubscriptionStatus>(::status.name) ?: SubscriptionStatus.SUBSCRIBED
164187
}
165188
set(value) {
166189
setEnumProperty(::status.name, value)

0 commit comments

Comments
 (0)