Skip to content

Commit

Permalink
fix(AdminSdkFcmSender): make sure ttl is vaild
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgxvii committed Jun 30, 2023
1 parent 42d2ab5 commit 03de1c3
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
@Slf4j
@SuppressWarnings({"PMD.DataflowAnomalyAnalysis", "PMD.AvoidDuplicateLiterals"})
public class AdminSdkFcmSender implements FcmSender {
static final int DEFAULT_TIME_TO_LIVE = 2_419_200; // 28 days in seconds

public AdminSdkFcmSender() throws IOException {
// TODO also take config from application properties
Expand All @@ -81,7 +82,7 @@ public void send(FcmDownstreamMessage downstreamMessage) throws FirebaseMessagin
.setFcmOptions(FcmOptions.builder().build())
.setCondition(downstreamMessage.getCondition());

int ttl = downstreamMessage.getTimeToLive() * 1000; // Convert to milliseconds
int ttl = getValidTtlMillis(downstreamMessage.getTimeToLive());

if (downstreamMessage instanceof FcmNotificationMessage) {
FcmNotificationMessage notificationMessage = (FcmNotificationMessage) downstreamMessage;
Expand Down Expand Up @@ -260,4 +261,11 @@ private String getString(Object obj) {
public boolean doesProvideDeliveryReceipt() {
return false;
}

public int getValidTtlMillis(int ttl) {
// Converts ttl from seconds to milliseconds and makes sure this is less than 28 days
int ttlSeconds = ttl >= 0 && ttl <= DEFAULT_TIME_TO_LIVE ? ttl : DEFAULT_TIME_TO_LIVE;
return ttlSeconds * 1000;
}

}

0 comments on commit 03de1c3

Please sign in to comment.