Send push notifications if websockets close before all messages are delivered

This commit is contained in:
Jon Chambers
2020-10-27 16:02:55 -04:00
committed by GitHub
parent ae566dca98
commit 05d9ec673e
8 changed files with 48 additions and 9 deletions

View File

@@ -138,21 +138,24 @@ public class MessageSender implements Managed {
throw new AssertionError();
}
final boolean clientPresent = clientPresenceManager.isPresent(account.getUuid(), device.getId());
final boolean clientPresent;
if (online) {
clientPresent = clientPresenceManager.isPresent(account.getUuid(), device.getId());
if (clientPresent) {
messagesManager.insertEphemeral(account.getUuid(), device.getId(), message);
}
} else {
messagesManager.insert(account.getUuid(), device.getId(), message);
// We check for client presence after inserting the message to take a conservative view of notifications. If the
// client wasn't present at the time of insertion but is now, they'll retrieve the message. If they were present
// but disconnected before the message was delivered, we should send a notification.
clientPresent = clientPresenceManager.isPresent(account.getUuid(), device.getId());
if (!clientPresent) {
if (!Util.isEmpty(device.getGcmId())) {
sendGcmNotification(account, device);
} else if (!Util.isEmpty(device.getApnId()) || !Util.isEmpty(device.getVoipApnId())) {
sendApnNotification(account, device);
}
sendNewMessageNotification(account, device);
}
}
@@ -164,6 +167,14 @@ public class MessageSender implements Managed {
Metrics.counter(SEND_COUNTER_NAME, tags).increment();
}
public void sendNewMessageNotification(final Account account, final Device device) {
if (!Util.isEmpty(device.getGcmId())) {
sendGcmNotification(account, device);
} else if (!Util.isEmpty(device.getApnId()) || !Util.isEmpty(device.getVoipApnId())) {
sendApnNotification(account, device);
}
}
private void sendGcmNotification(Account account, Device device) {
GcmMessage gcmMessage = new GcmMessage(device.getGcmId(), account.getNumber(),
(int)device.getId(), GcmMessage.Type.NOTIFICATION, Optional.empty());