Skip to content

Commit e266904

Browse files
committed
Correctly parse multiple subscriptions in create user response
* The subscriptions in the create user response are not returned in the order that they are sent. We cannot rely on ordering to match sent and returned subscriptions. So, we do our best to use data to match them. * Fixes a bug where a push subscription and email/sms subscription are in the same request and the SDK extracts the wrong subscription ID for push.
1 parent c6ce5b2 commit e266904

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,36 @@ internal class LoginUserOperationExecutor(
187187
propertiesModel.setStringProperty(PropertiesModel::onesignalId.name, backendOneSignalId, ModelChangeTags.HYDRATE)
188188
}
189189

190-
for (index in subscriptionList.indices) {
191-
if (index >= response.subscriptions.size) {
192-
break
190+
val backendSubscriptions = response.subscriptions.toMutableSet()
191+
192+
for (pair in subscriptionList) {
193+
// Find the corresponding subscription (subscriptions are not returned in the order they are sent)
194+
// 1. Start by matching the subscription ID
195+
var backendSubscription = backendSubscriptions.firstOrNull { it.id == pair.first }
196+
// 2. If ID fails, match the token, this should always succeed for email or sms
197+
if (backendSubscription == null) {
198+
backendSubscription = backendSubscriptions.firstOrNull { it.token == pair.second.token && !it.token.isNullOrBlank() }
199+
}
200+
// 3. Match by type. By this point, only a single push subscription should remain, at most
201+
if (backendSubscription == null) {
202+
backendSubscription = backendSubscriptions.firstOrNull { it.type == pair.second.type }
193203
}
194204

195-
val backendSubscription = response.subscriptions[index]
205+
if (backendSubscription != null) {
206+
idTranslations[pair.first] = backendSubscription.id!!
196207

197-
idTranslations[subscriptionList[index].first] = backendSubscription.id!!
208+
if (_configModelStore.model.pushSubscriptionId == pair.first) {
209+
_configModelStore.model.pushSubscriptionId = backendSubscription.id
210+
}
198211

199-
if (_configModelStore.model.pushSubscriptionId == subscriptionList[index].first) {
200-
_configModelStore.model.pushSubscriptionId = backendSubscription.id
212+
val subscriptionModel = _subscriptionsModelStore.get(pair.first)
213+
subscriptionModel?.setStringProperty(SubscriptionModel::id.name, backendSubscription.id!!, ModelChangeTags.HYDRATE)
214+
} else {
215+
Logging.error("LoginUserOperationExecutor.createUser response is missing subscription data for ${pair.first}")
201216
}
202217

203-
val subscriptionModel = _subscriptionsModelStore.get(subscriptionList[index].first)
204-
subscriptionModel?.setStringProperty(SubscriptionModel::id.name, backendSubscription.id, ModelChangeTags.HYDRATE)
218+
// Remove the processed backend subscription
219+
backendSubscriptions.remove(backendSubscription)
205220
}
206221

207222
val wasPossiblyAnUpsert = identities.isNotEmpty()

0 commit comments

Comments
 (0)