From 7656a8b298d15b292252b6a7b69685dff14a72ae Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 31 May 2025 11:54:23 +0800 Subject: [PATCH] :bug: Trying to fix the message notification... --- .../Account/NotificationService.cs | 49 ++++++++++++++----- DysonNetwork.Sphere/Chat/ChatService.cs | 6 +-- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/DysonNetwork.Sphere/Account/NotificationService.cs b/DysonNetwork.Sphere/Account/NotificationService.cs index 1599408..98dc70f 100644 --- a/DysonNetwork.Sphere/Account/NotificationService.cs +++ b/DysonNetwork.Sphere/Account/NotificationService.cs @@ -158,9 +158,18 @@ public class NotificationService var accounts = await _db.Accounts.ToListAsync(); var notifications = accounts.Select(x => { - notification.Account = x; - notification.AccountId = x.Id; - return notification; + var newNotification = new 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(); await _db.BulkInsertAsync(notifications); } @@ -183,9 +192,18 @@ public class NotificationService { var notifications = accounts.Select(x => { - notification.Account = x; - notification.AccountId = x.Id; - return notification; + var newNotification = new 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(); await _db.BulkInsertAsync(notifications); } @@ -222,16 +240,22 @@ public class NotificationService notification.Content ?? string.Empty).Trim(); } - await _fcm.SendAsync(new + await _fcm.SendAsync(new Dictionary { - message = new + ["message"] = new Dictionary { - token = subscription.DeviceToken, - notification = new + ["token"] = subscription.DeviceToken, + ["notification"] = new Dictionary { - title = notification.Title ?? string.Empty, body + ["title"] = notification.Title ?? string.Empty, + ["body"] = body }, - data = notification.Meta ?? new Dictionary() + ["data"] = new Dictionary + { + ["id"] = notification.Id, + ["topic"] = notification.Topic, + ["meta"] = notification.Meta ?? new Dictionary() + } } }); break; @@ -242,6 +266,7 @@ public class NotificationService await _apns.SendAsync(new Dictionary { + ["topic"] = notification.Topic, ["aps"] = new Dictionary { ["alert"] = new Dictionary diff --git a/DysonNetwork.Sphere/Chat/ChatService.cs b/DysonNetwork.Sphere/Chat/ChatService.cs index 0c89511..caa1218 100644 --- a/DysonNetwork.Sphere/Chat/ChatService.cs +++ b/DysonNetwork.Sphere/Chat/ChatService.cs @@ -66,14 +66,13 @@ public class ChatService( var scopedCrs = scope.ServiceProvider.GetRequiredService(); var roomSubject = room.Realm is not null ? $"{room.Name}, {room.Realm.Name}" : room.Name; - var tasks = new List(); var members = await scopedCrs.ListRoomMembers(room.Id); var notification = new Notification { Topic = "messages.new", - Title = $"{sender.Nick ?? sender.Account?.Nick ?? "Unknown"} ({roomSubject})", + Title = $"{sender.Nick ?? sender.Account.Nick ?? "Unknown"} ({roomSubject})", Content = !string.IsNullOrEmpty(message.Content) ? message.Content[..Math.Min(message.Content.Length, 100)] : "", @@ -106,9 +105,8 @@ public class ChatService( logger.LogInformation($"Trying to deliver message to {accountsToNotify.Count} accounts..."); // Only send notifications if there are accounts to notify 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."); }