Skip to content

Commit

Permalink
Merge pull request #765 from /issues/764-refactor-PushSendingWorker
Browse files Browse the repository at this point in the history
Fix #764: Extract android code in PushSendingWorker
  • Loading branch information
banterCZ authored Feb 1, 2024
2 parents f6adc37 + 95b85c6 commit 47eae2f
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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<Long> 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

/**
Expand Down

0 comments on commit 47eae2f

Please sign in to comment.