From 688b0177d0e0a31e7eb780362ea43d9bc5993dad Mon Sep 17 00:00:00 2001 From: Roman Strobl Date: Thu, 12 Dec 2024 14:01:40 +0800 Subject: [PATCH] Support for more than 2 activations --- .../push/service/DeviceRegistrationService.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/powerauth-push-server/src/main/java/io/getlime/push/service/DeviceRegistrationService.java b/powerauth-push-server/src/main/java/io/getlime/push/service/DeviceRegistrationService.java index 8c588e76..ceac92b7 100644 --- a/powerauth-push-server/src/main/java/io/getlime/push/service/DeviceRegistrationService.java +++ b/powerauth-push-server/src/main/java/io/getlime/push/service/DeviceRegistrationService.java @@ -69,21 +69,25 @@ public void createOrUpdateDevice(final CreateDeviceRequest requestObject, final final String activationId = requestObject.getActivationId(); final List devices = lookupDeviceRegistrations(appId, activationId, pushToken); - final PushDeviceRegistrationEntity device; + PushDeviceRegistrationEntity device = null; if (devices.isEmpty()) { // The device registration is new, create a new entity. logger.info("Creating new device registration: app ID: {}, activation ID: {}, platform: {}", requestObject.getAppId(), requestObject.getActivationId(), platform); device = initDeviceRegistrationEntity(appCredentials, pushToken); } else { - // An existing row was found by one of the lookup methods. This means that either: + // An existing row(s) were found by one of the lookup methods. This means that either: // 1. A row with same activation ID and push token is updated, in this case only the last registration timestamp changes. // 2. A row with same activation ID but different push token is updated. A new push token has been issued by Google or Apple for an activation. // 3. A row with different activation ID but same push token is created. The user removed an activation and created a new one, the push token remains the same, - // or the user registers more activations with the same push token. - if (devices.get(0).getActivationId().equals(activationId)) { + // or the user registered multiple activations with the same push token. + for (PushDeviceRegistrationEntity existingDevice: devices) { + if (existingDevice.getActivationId().equals(activationId)) { + device = existingDevice; + } + } + if (device != null) { logger.info("Updating existing device registration: app ID: {}, activation ID: {}, platform: {}", requestObject.getAppId(), requestObject.getActivationId(), platform); // Update an existing registration record - device = devices.get(0); updateDeviceRegistrationEntity(device, appCredentials, pushToken); } else { logger.info("Creating new device registration for a new activation: app ID: {}, activation ID: {}, platform: {}", requestObject.getAppId(), requestObject.getActivationId(), platform);