🐛 Trying to fix message notification again...

This commit is contained in:
2025-05-30 22:49:03 +08:00
parent b7960e3060
commit 472221302d
2 changed files with 26 additions and 11 deletions

View File

@ -181,12 +181,19 @@ public class NotificationService
{ {
if (save) if (save)
{ {
var notifications = accounts.Select(x => // Create new notification objects for each account instead of reusing
var notifications = accounts.Select(x => new Notification
{ {
notification.Account = x; Topic = notification.Topic,
notification.AccountId = x.Id; Title = notification.Title,
return notification; Subtitle = notification.Subtitle,
Content = notification.Content,
Meta = notification.Meta != null ? new Dictionary<string, object>(notification.Meta) : null,
Priority = notification.Priority,
Account = x,
AccountId = x.Id
}).ToList(); }).ToList();
await _db.BulkInsertAsync(notifications); await _db.BulkInsertAsync(notifications);
} }
@ -194,12 +201,20 @@ public class NotificationService
var subscribers = await _db.NotificationPushSubscriptions var subscribers = await _db.NotificationPushSubscriptions
.Where(s => accountsId.Contains(s.AccountId)) .Where(s => accountsId.Contains(s.AccountId))
.ToListAsync(); .ToListAsync();
var tasks = new List<Task>();
foreach (var subscriber in subscribers) var tasks = (from subscriber in subscribers
{ let notificationCopy = new Notification
notification.AccountId = subscriber.AccountId; {
tasks.Add(_PushSingleNotification(notification, subscriber)); Id = notification.Id,
} Topic = notification.Topic,
Title = notification.Title,
Subtitle = notification.Subtitle,
Content = notification.Content,
Meta = notification.Meta != null ? new Dictionary<string, object>(notification.Meta) : null,
Priority = notification.Priority,
AccountId = subscriber.AccountId
}
select _PushSingleNotification(notificationCopy, subscriber)).ToList();
await Task.WhenAll(tasks); await Task.WhenAll(tasks);
} }

View File

@ -46,7 +46,7 @@ public class ChatService(
{ {
// Log the exception properly // Log the exception properly
// Consider using ILogger or your logging framework // Consider using ILogger or your logging framework
logger.LogError($"Error when delivering message: {ex.Message}"); logger.LogError($"Error when delivering message: {ex.Message} {ex.StackTrace}");
} }
}); });