From 95b85c6d5065feda16352bc2670264a059848f4e Mon Sep 17 00:00:00 2001 From: Lubos Racansky Date: Wed, 31 Jan 2024 11:41:01 +0100 Subject: [PATCH] Fix #764: Extract android code in PushSendingWorker --- .../push/service/PushSendingWorker.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java b/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java index 844d06022..b352c315c 100644 --- a/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java +++ b/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java @@ -231,17 +231,8 @@ private Message buildAndroidMessage(final PushMessageBody pushMessageBody, final final AndroidConfig.Builder androidConfigBuilder = AndroidConfig.builder() .setCollapseKey(pushMessageBody.getCollapseKey()); - // Calculate TTL and set it if the TTL is within reasonable limits - final Instant validUntil = pushMessageBody.getValidUntil(); - if (validUntil != null) { - final long validUntilMs = validUntil.toEpochMilli(); - final long currentTimeMs = System.currentTimeMillis(); - final long ttlInSeconds = (validUntilMs - currentTimeMs) / 1000; - - if (ttlInSeconds > 0 && ttlInSeconds < ANDROID_TTL_SECONDS_MAX) { - androidConfigBuilder.setTtl(ttlInSeconds); - } - } + calculateTtl(pushMessageBody.getValidUntil()) + .ifPresent(androidConfigBuilder::setTtl); final AndroidNotification.Priority deliveryPriority = (Priority.NORMAL == priority) ? AndroidNotification.Priority.DEFAULT : AndroidNotification.Priority.HIGH; @@ -267,7 +258,7 @@ private Message buildAndroidMessage(final PushMessageBody pushMessageBody, final if (pushServiceConfiguration.isFcmDataNotificationOnly()) { // notification only through data map data.put(FCM_NOTIFICATION_KEY, fcmConverter.convertNotificationToString(notification)); - } else if (attributes == null || !attributes.getSilent()) { // if there are no attributes, assume the message is not silent + } else if (isMessageNotSilent(attributes)) { androidConfigBuilder.setNotification(notification); } @@ -278,6 +269,28 @@ private Message buildAndroidMessage(final PushMessageBody pushMessageBody, final .build(); } + private static boolean isMessageNotSilent(final PushMessageAttributes attributes) { + // if there are no attributes, assume the message is not silent + return attributes == null || !attributes.getSilent(); + } + + /** + * Calculate TTL and return it if the TTL is within reasonable limits. + * + * @param validUntil Valid until. + * @return TTL in seconds or empty. + */ + private static Optional calculateTtl(final Instant validUntil) { + if (validUntil != null) { + final long ttlInSeconds = Duration.between(Instant.now(), validUntil).toSeconds(); + + if (ttlInSeconds > 0 && ttlInSeconds < ANDROID_TTL_SECONDS_MAX) { + return Optional.of(ttlInSeconds); + } + } + return Optional.empty(); + } + // iOS related methods /**