🐛 Trying to fix the message notification...

This commit is contained in:
LittleSheep 2025-05-31 11:54:23 +08:00
parent 1024721e0e
commit 7656a8b298
2 changed files with 39 additions and 16 deletions

View File

@ -158,9 +158,18 @@ public class NotificationService
var accounts = await _db.Accounts.ToListAsync(); var accounts = await _db.Accounts.ToListAsync();
var notifications = accounts.Select(x => var notifications = accounts.Select(x =>
{ {
notification.Account = x; var newNotification = new Notification
notification.AccountId = x.Id; {
return notification; Topic = notification.Topic,
Title = notification.Title,
Subtitle = notification.Subtitle,
Content = notification.Content,
Meta = notification.Meta,
Priority = notification.Priority,
Account = x,
AccountId = x.Id
};
return newNotification;
}).ToList(); }).ToList();
await _db.BulkInsertAsync(notifications); await _db.BulkInsertAsync(notifications);
} }
@ -183,9 +192,18 @@ public class NotificationService
{ {
var notifications = accounts.Select(x => var notifications = accounts.Select(x =>
{ {
notification.Account = x; var newNotification = new Notification
notification.AccountId = x.Id; {
return notification; Topic = notification.Topic,
Title = notification.Title,
Subtitle = notification.Subtitle,
Content = notification.Content,
Meta = notification.Meta,
Priority = notification.Priority,
Account = x,
AccountId = x.Id
};
return newNotification;
}).ToList(); }).ToList();
await _db.BulkInsertAsync(notifications); await _db.BulkInsertAsync(notifications);
} }
@ -222,16 +240,22 @@ public class NotificationService
notification.Content ?? string.Empty).Trim(); notification.Content ?? string.Empty).Trim();
} }
await _fcm.SendAsync(new await _fcm.SendAsync(new Dictionary<string, object>
{ {
message = new ["message"] = new Dictionary<string, object>
{ {
token = subscription.DeviceToken, ["token"] = subscription.DeviceToken,
notification = new ["notification"] = new Dictionary<string, object>
{ {
title = notification.Title ?? string.Empty, body ["title"] = notification.Title ?? string.Empty,
["body"] = body
}, },
data = notification.Meta ?? new Dictionary<string, object>() ["data"] = new Dictionary<string, object>
{
["id"] = notification.Id,
["topic"] = notification.Topic,
["meta"] = notification.Meta ?? new Dictionary<string, object>()
}
} }
}); });
break; break;
@ -242,6 +266,7 @@ public class NotificationService
await _apns.SendAsync(new Dictionary<string, object> await _apns.SendAsync(new Dictionary<string, object>
{ {
["topic"] = notification.Topic,
["aps"] = new Dictionary<string, object> ["aps"] = new Dictionary<string, object>
{ {
["alert"] = new Dictionary<string, object> ["alert"] = new Dictionary<string, object>

View File

@ -66,14 +66,13 @@ public class ChatService(
var scopedCrs = scope.ServiceProvider.GetRequiredService<ChatRoomService>(); var scopedCrs = scope.ServiceProvider.GetRequiredService<ChatRoomService>();
var roomSubject = room.Realm is not null ? $"{room.Name}, {room.Realm.Name}" : room.Name; var roomSubject = room.Realm is not null ? $"{room.Name}, {room.Realm.Name}" : room.Name;
var tasks = new List<Task>();
var members = await scopedCrs.ListRoomMembers(room.Id); var members = await scopedCrs.ListRoomMembers(room.Id);
var notification = new Notification var notification = new Notification
{ {
Topic = "messages.new", Topic = "messages.new",
Title = $"{sender.Nick ?? sender.Account?.Nick ?? "Unknown"} ({roomSubject})", Title = $"{sender.Nick ?? sender.Account.Nick ?? "Unknown"} ({roomSubject})",
Content = !string.IsNullOrEmpty(message.Content) Content = !string.IsNullOrEmpty(message.Content)
? message.Content[..Math.Min(message.Content.Length, 100)] ? message.Content[..Math.Min(message.Content.Length, 100)]
: "<attachments>", : "<attachments>",
@ -106,9 +105,8 @@ public class ChatService(
logger.LogInformation($"Trying to deliver message to {accountsToNotify.Count} accounts..."); logger.LogInformation($"Trying to deliver message to {accountsToNotify.Count} accounts...");
// Only send notifications if there are accounts to notify // Only send notifications if there are accounts to notify
if (accountsToNotify.Count > 0) if (accountsToNotify.Count > 0)
tasks.Add(scopedNty.SendNotificationBatch(notification, accountsToNotify, save: false)); await scopedNty.SendNotificationBatch(notification, accountsToNotify, save: false);
await Task.WhenAll(tasks);
logger.LogInformation($"Delivered message to {accountsToNotify.Count} accounts."); logger.LogInformation($"Delivered message to {accountsToNotify.Count} accounts.");
} }