Harden push notification delivery

Keep encrypted message content out of push payloads and improve cross-platform notification data. Add token registration validation and per-session unregister support, batch and deduplicate sends, remove only confirmed unregistered tokens, and protect queued retries with leases. Add delivery tests and document the push API and Firebase setup.
This commit is contained in:
Artemy
2026-10-17 19:53:34 +07:00
parent 9aa7e017e2
commit e060b2bcbb
21 changed files with 16353 additions and 132 deletions
@@ -6,44 +6,58 @@ using Microsoft.EntityFrameworkCore;
namespace Govor.API.Hubs.Infrastructure;
public class ChatPushDispatcher(GovorDbContext context, IChatNotificationService notifier,
INowDateTimeProvider dateTimeProvider)
INowDateTimeProvider dateTimeProvider, ILogger<ChatPushDispatcher> logger = null)
{
public async Task DispatchAsync(CancellationToken cancellationToken = default)
{
var now = dateTimeProvider.Now;
var pending = await context.ChatPushNotifications.AsNoTracking()
.Where(n => n.NextAttemptAt <= now).OrderBy(n => n.NextAttemptAt)
.Take(20).Select(n => n.MessageId).ToListAsync(cancellationToken);
.Take(20).Select(n => new { n.MessageId, n.Attempts }).ToListAsync(cancellationToken);
foreach (var id in pending)
foreach (var item in pending)
{
cancellationToken.ThrowIfCancellationRequested();
var id = item.MessageId;
now = dateTimeProvider.Now;
// Conditional update claims a two-minute lease, including across server instances.
// An interrupted worker leaves a retryable row rather than losing the notification.
var claimed = await context.ChatPushNotifications
.Where(n => n.MessageId == id && n.NextAttemptAt <= now)
.Where(n => n.MessageId == id && n.NextAttemptAt <= now && n.Attempts == item.Attempts)
.ExecuteUpdateAsync(s => s.SetProperty(n => n.NextAttemptAt, now.AddMinutes(2))
.SetProperty(n => n.Attempts, n => n.Attempts + 1), cancellationToken);
if (claimed == 0)
continue;
var message = await context.Messages.AsNoTracking()
.Where(m => m.Id == id).Select(m => new UserMessageResponse
{
MessageId = m.Id, SenderId = m.SenderId, RecipientId = m.RecipientId,
RecipientType = m.RecipientType, EncryptedContent = m.EncryptedContent,
SentAt = m.SentAt, ReplyToMessageId = m.ReplyToMessageId
}).FirstOrDefaultAsync(cancellationToken);
var delivered = message is null || await notifier.DeliverPushAsync(message);
var attempt = item.Attempts + 1;
// An old worker must not complete or reschedule a newer worker's lease.
var owned = context.ChatPushNotifications.Where(n => n.MessageId == id && n.Attempts == attempt);
var delivered = false;
try
{
var message = await context.Messages.AsNoTracking()
.Where(m => m.Id == id).Select(m => new UserMessageResponse
{
MessageId = m.Id, SenderId = m.SenderId, RecipientId = m.RecipientId,
RecipientType = m.RecipientType, EncryptedContent = m.EncryptedContent,
SentAt = m.SentAt, ReplyToMessageId = m.ReplyToMessageId
}).FirstOrDefaultAsync(cancellationToken);
delivered = message is null || await notifier.DeliverPushAsync(message);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception ex)
{
logger?.LogError(ex, "Push attempt failed for message {MessageId}; delivery will be retried.", id);
}
if (delivered)
await context.ChatPushNotifications.Where(n => n.MessageId == id)
.ExecuteDeleteAsync(cancellationToken);
await owned.ExecuteDeleteAsync(cancellationToken);
else
{
var attempts = await context.ChatPushNotifications.Where(n => n.MessageId == id)
.Select(n => n.Attempts).FirstOrDefaultAsync(cancellationToken);
var retryAt = dateTimeProvider.Now.AddSeconds(Math.Min(300, Math.Pow(2, Math.Min(attempts, 9))));
await context.ChatPushNotifications.Where(n => n.MessageId == id)
var retryAt = dateTimeProvider.Now.AddSeconds(Math.Min(300, Math.Pow(2, Math.Min(attempt, 9))));
await owned
.ExecuteUpdateAsync(s => s.SetProperty(n => n.NextAttemptAt, retryAt), cancellationToken);
}
}