mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 12:06:53 +00:00
Allow differing TTLs by notification type
This commit is contained in:
1 parent
539a84d0d0
commit
51f6e57bbd
10 files changed
+71
-36
No files matched your search
@@ -719,7 +719,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
|
||||
registrationRecoveryPasswordsManager, accountLockExecutor, messagePollExecutor,
|
||||
retryExecutor, clock, config.getLinkDeviceSecretConfiguration().secret().value());
|
||||
RemoteConfigsManager remoteConfigsManager = new RemoteConfigsManager(remoteConfigs);
|
||||
APNSender apnSender = new APNSender(apnSenderExecutor, config.getApnConfiguration());
|
||||
APNSender apnSender = new APNSender(apnSenderExecutor, Clock.systemUTC(), config.getApnConfiguration());
|
||||
FcmSender fcmSender = new FcmSender(fcmSenderExecutor, config.getFcmConfiguration().credentials().value());
|
||||
PushNotificationScheduler pushNotificationScheduler = new PushNotificationScheduler(pushSchedulerCluster,
|
||||
apnSender, fcmSender, accountsManager, 0, 0, retryExecutor);
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
@@ -31,6 +32,8 @@ import org.whispersystems.textsecuregcm.configuration.ApnConfiguration;
|
||||
public class APNSender implements Managed, PushNotificationSender {
|
||||
|
||||
private final ExecutorService executor;
|
||||
private final Clock clock;
|
||||
|
||||
private final String bundleId;
|
||||
private final ApnsClient apnsClient;
|
||||
|
||||
@@ -45,17 +48,19 @@ public class APNSender implements Managed, PushNotificationSender {
|
||||
.setContentAvailable(true)
|
||||
.build();
|
||||
|
||||
// See https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns
|
||||
@VisibleForTesting
|
||||
static final Instant MAX_EXPIRATION = Instant.ofEpochMilli(Integer.MAX_VALUE * 1000L);
|
||||
static final Duration DEFAULT_TTL = Duration.ofDays(30);
|
||||
|
||||
private static final String APNS_CA_FILENAME = "apns-certificates.pem";
|
||||
|
||||
private static final Timer SEND_NOTIFICATION_TIMER = Metrics.timer(name(APNSender.class, "sendNotification"));
|
||||
|
||||
public APNSender(ExecutorService executor, ApnConfiguration configuration)
|
||||
public APNSender(final ExecutorService executor, final Clock clock, final ApnConfiguration configuration)
|
||||
throws IOException, NoSuchAlgorithmException, InvalidKeyException
|
||||
{
|
||||
this.executor = executor;
|
||||
this.clock = clock;
|
||||
this.bundleId = configuration.bundleId();
|
||||
this.apnsClient = new ApnsClientBuilder().setSigningKey(
|
||||
ApnsSigningKey.loadFromInputStream(new ByteArrayInputStream(configuration.signingKey().value().getBytes()),
|
||||
@@ -66,8 +71,9 @@ public class APNSender implements Managed, PushNotificationSender {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public APNSender(ExecutorService executor, ApnsClient apnsClient, String bundleId) {
|
||||
public APNSender(final ExecutorService executor, final ApnsClient apnsClient, final String bundleId, final Clock clock) {
|
||||
this.executor = executor;
|
||||
this.clock = clock;
|
||||
this.apnsClient = apnsClient;
|
||||
this.bundleId = bundleId;
|
||||
}
|
||||
@@ -117,7 +123,7 @@ public class APNSender implements Managed, PushNotificationSender {
|
||||
return apnsClient.sendNotification(new SimpleApnsPushNotification(notification.deviceToken(),
|
||||
bundleId,
|
||||
payload,
|
||||
MAX_EXPIRATION,
|
||||
clock.instant().plus(notification.ttl() != null ? notification.ttl() : DEFAULT_TTL),
|
||||
deliveryPriority,
|
||||
pushType,
|
||||
collapseId))
|
||||
|
||||
@@ -23,6 +23,7 @@ import io.micrometer.core.instrument.Timer;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -37,6 +38,9 @@ public class FcmSender implements PushNotificationSender {
|
||||
private final ExecutorService executor;
|
||||
private final FirebaseMessaging firebaseMessagingClient;
|
||||
|
||||
// See https://firebase.google.com/docs/cloud-messaging/customize-messages/setting-message-lifespan
|
||||
private static final long DEFAULT_TTL_MILLIS = Duration.ofDays(28).toMillis();
|
||||
|
||||
private static final Timer SEND_NOTIFICATION_TIMER = Metrics.timer(name(FcmSender.class, "sendNotification"));
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FcmSender.class);
|
||||
@@ -82,6 +86,7 @@ public class FcmSender implements PushNotificationSender {
|
||||
.setToken(pushNotification.deviceToken())
|
||||
.setAndroidConfig(AndroidConfig.builder()
|
||||
.setPriority(pushNotification.urgent() ? AndroidConfig.Priority.HIGH : AndroidConfig.Priority.NORMAL)
|
||||
.setTtl(pushNotification.ttl() != null ? pushNotification.ttl().toMillis() : DEFAULT_TTL_MILLIS)
|
||||
.build());
|
||||
|
||||
final String key = switch (pushNotification.notificationType()) {
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
package org.whispersystems.textsecuregcm.push;
|
||||
|
||||
import java.time.Duration;
|
||||
import javax.annotation.Nullable;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public record PushNotification(String deviceToken,
|
||||
TokenType tokenType,
|
||||
@@ -15,7 +16,14 @@ public record PushNotification(String deviceToken,
|
||||
@Nullable Object data,
|
||||
@Nullable Account destination,
|
||||
@Nullable Device destinationDevice,
|
||||
boolean urgent) {
|
||||
boolean urgent,
|
||||
@Nullable Duration ttl) {
|
||||
|
||||
// APNs allows up to 30 days, but FCM allows a max of 28. See:
|
||||
//
|
||||
// - https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns
|
||||
// - https://firebase.google.com/docs/cloud-messaging/customize-messages/setting-message-lifespan
|
||||
private static final Duration MAX_TTL = Duration.ofDays(28);
|
||||
|
||||
public enum NotificationType {
|
||||
NOTIFICATION,
|
||||
@@ -28,4 +36,10 @@ public record PushNotification(String deviceToken,
|
||||
FCM,
|
||||
APN
|
||||
}
|
||||
|
||||
public PushNotification {
|
||||
if (ttl != null && ttl.compareTo(MAX_TTL) > 0) {
|
||||
throw new IllegalArgumentException("TTL must not be longer than " + MAX_TTL);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-4
@@ -52,11 +52,12 @@ public class PushNotificationManager {
|
||||
final Pair<String, PushNotification.TokenType> tokenAndType = getToken(device);
|
||||
|
||||
return sendNotification(new PushNotification(tokenAndType.first(), tokenAndType.second(),
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destination, device, urgent));
|
||||
PushNotification.NotificationType.NOTIFICATION, null, destination, device, urgent, null));
|
||||
}
|
||||
|
||||
public CompletableFuture<SendPushNotificationResult> sendRegistrationChallengeNotification(final String deviceToken, final PushNotification.TokenType tokenType, final String challengeToken) {
|
||||
return sendNotification(new PushNotification(deviceToken, tokenType, PushNotification.NotificationType.CHALLENGE, challengeToken, null, null, true))
|
||||
return sendNotification(new PushNotification(deviceToken, tokenType, PushNotification.NotificationType.CHALLENGE, challengeToken, null, null, true,
|
||||
null))
|
||||
.thenApply(maybeResponse -> maybeResponse.orElseThrow(() -> new AssertionError("Responses must be present for urgent notifications")));
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ public class PushNotificationManager {
|
||||
final Pair<String, PushNotification.TokenType> tokenAndType = getToken(device);
|
||||
|
||||
return sendNotification(new PushNotification(tokenAndType.first(), tokenAndType.second(),
|
||||
PushNotification.NotificationType.RATE_LIMIT_CHALLENGE, challengeToken, destination, device, true))
|
||||
PushNotification.NotificationType.RATE_LIMIT_CHALLENGE, challengeToken, destination, device, true, null))
|
||||
.thenApply(maybeResponse -> maybeResponse.orElseThrow(() -> new AssertionError("Responses must be present for urgent notifications")));
|
||||
}
|
||||
|
||||
@@ -77,7 +78,7 @@ public class PushNotificationManager {
|
||||
|
||||
return sendNotification(new PushNotification(tokenAndType.first(), tokenAndType.second(),
|
||||
PushNotification.NotificationType.ATTEMPT_LOGIN_NOTIFICATION_HIGH_PRIORITY,
|
||||
context, destination, device, true))
|
||||
context, destination, device, true, null))
|
||||
.thenApply(maybeResponse -> maybeResponse.orElseThrow(() -> new AssertionError("Responses must be present for urgent notifications")));
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -311,7 +311,7 @@ public class PushNotificationScheduler implements Managed {
|
||||
return pushSchedulingCluster.withCluster(connection -> connection.async().set(
|
||||
getLastBackgroundNotificationTimestampKey(account, device),
|
||||
String.valueOf(clock.millis()), new SetArgs().ex(BACKGROUND_NOTIFICATION_PERIOD)))
|
||||
.thenCompose(ignored -> sender.sendNotification(new PushNotification(pushToken, tokenType, PushNotification.NotificationType.NOTIFICATION, null, account, device, false)))
|
||||
.thenCompose(ignored -> sender.sendNotification(new PushNotification(pushToken, tokenType, PushNotification.NotificationType.NOTIFICATION, null, account, device, false, null)))
|
||||
.thenAccept(response -> Metrics.counter(BACKGROUND_NOTIFICATION_SENT_COUNTER_NAME,
|
||||
ACCEPTED_TAG, String.valueOf(response.accepted()))
|
||||
.increment())
|
||||
@@ -333,7 +333,7 @@ public class PushNotificationScheduler implements Managed {
|
||||
null,
|
||||
account,
|
||||
device,
|
||||
true);
|
||||
true, null);
|
||||
|
||||
final PushNotificationSender pushNotificationSender = isApnsDevice ? apnSender : fcmSender;
|
||||
|
||||
|
||||
+1
-1
@@ -349,7 +349,7 @@ public record CommandDependencies(
|
||||
zkReceiptOperations,
|
||||
issuedReceiptsManager);
|
||||
|
||||
APNSender apnSender = new APNSender(apnSenderExecutor, configuration.getApnConfiguration());
|
||||
APNSender apnSender = new APNSender(apnSenderExecutor, Clock.systemUTC(), configuration.getApnConfiguration());
|
||||
FcmSender fcmSender = new FcmSender(fcmSenderExecutor, configuration.getFcmConfiguration().credentials().value());
|
||||
PushNotificationScheduler pushNotificationScheduler = new PushNotificationScheduler(pushSchedulerCluster,
|
||||
apnSender, fcmSender, accountsManager, 0, 0, retryExecutor);
|
||||
|
||||
Reference in new issue
Block a user