Skip to content

Commit ba88ec2

Browse files
committed
recover from invalid Operations missing onesignalId
* When uncaching an Operation that is not login-related, check for the existence of onesignalId. * Note: The login-related Operations should still have onesignalId but they can be sent without it. It is also less likely for onesignalId to be null based on when these Operations are created. * This is to remedy a rare case that an Operation could be missing the onesignalId, which would continuously cause crashes when the Operation is processed. The particular reported Operation is an UpdateSubscriptionOperation.
1 parent 429c1c9 commit ba88ec2

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl/OperationModelStore.kt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper
3838
return null
3939
}
4040

41-
if (!jsonObject.has(Operation::name.name)) {
42-
Logging.error("jsonObject must have '${Operation::name.name}' attribute")
41+
if (!isValidOperation(jsonObject)) {
4342
return null
4443
}
4544

@@ -69,4 +68,34 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper
6968

7069
return operation
7170
}
71+
72+
/**
73+
* Checks if a JSONObject is a valid Operation. Contains a check for onesignalId.
74+
* This is a rare case that a cached Operation is missing the onesignalId,
75+
* which would continuously cause crashes when the Operation is processed.
76+
*
77+
* @param jsonObject The [JSONObject] that represents an Operation
78+
*/
79+
private fun isValidOperation(jsonObject: JSONObject): Boolean {
80+
if (!jsonObject.has(Operation::name.name)) {
81+
Logging.error("jsonObject must have '${Operation::name.name}' attribute")
82+
return false
83+
}
84+
85+
val operationName = jsonObject.getString(Operation::name.name)
86+
87+
val excluded =
88+
setOf(
89+
LoginUserOperationExecutor.LOGIN_USER,
90+
LoginUserFromSubscriptionOperationExecutor.LOGIN_USER_FROM_SUBSCRIPTION_USER,
91+
)
92+
93+
// Must have onesignalId if it is not one of the excluded operations above
94+
if (!jsonObject.has("onesignalId") && !excluded.contains(operationName)) {
95+
Logging.error("$operationName jsonObject must have 'onesignalId' attribute")
96+
return false
97+
}
98+
99+
return true
100+
}
72101
}

0 commit comments

Comments
 (0)