🔊 Add logs to notification pushing

This commit is contained in:
2025-05-31 19:54:10 +08:00
parent 6965744d5a
commit 28ff78d3e2
3 changed files with 32 additions and 13 deletions

View File

@ -226,6 +226,9 @@ public class NotificationService
{
try
{
_logger.LogDebug(
$"Pushing notification {notification.Topic} #{notification.Id} to device #{subscription.DeviceId}");
var body = string.Empty;
switch (subscription.Provider)
{
@ -245,7 +248,7 @@ public class NotificationService
["message"] = new Dictionary<string, object>
{
["token"] = subscription.DeviceToken,
["notification"] = new Dictionary<string, object>
["notification"] = new Dictionary<string, object>
{
["title"] = notification.Title ?? string.Empty,
["body"] = body
@ -265,14 +268,14 @@ public class NotificationService
throw new InvalidOperationException("The apple notification push service is not initialized.");
var alertDict = new Dictionary<string, object>();
if (!string.IsNullOrEmpty(notification.Title))
alertDict["title"] = notification.Title;
if (!string.IsNullOrEmpty(notification.Subtitle))
alertDict["subtitle"] = notification.Subtitle;
if (!string.IsNullOrEmpty(notification.Content))
alertDict["body"] = notification.Content;
var payload = new Dictionary<string, object>
{
["topic"] = notification.Topic,
@ -303,7 +306,12 @@ public class NotificationService
// Log the exception
// Consider implementing a retry mechanism
// Rethrow or handle as needed
_logger.LogError(
$"Failed to push notification #{notification.Id} to device... {ex.Message} {ex.StackTrace}");
throw new Exception($"Failed to send notification to {subscription.Provider}: {ex.Message}", ex);
}
_logger.LogInformation(
$"Pushed notification #{notification.Id} to device #{subscription.DeviceId}");
}
}

View File

@ -69,21 +69,31 @@ public class ChatService(
var members = await scopedCrs.ListRoomMembers(room.Id);
var metaDict =
new Dictionary<string, object>
{
["sender_name"] = sender.Nick ?? sender.Account.Nick,
["user_id"] = sender.AccountId,
["sender_id"] = sender.Id,
["message_id"] = message.Id,
["room_id"] = room.Id,
["images"] = message.Attachments
.Where(a => a.MimeType != null && a.MimeType.StartsWith("image"))
.Select(a => a.Id).ToList()
};
if (sender.Account.Profile is not { PictureId: null })
metaDict["pfp"] = sender.Account.Profile.PictureId;
if (!string.IsNullOrEmpty(room.Name))
metaDict["room_name"] = room.Name;
var notification = new Notification
{
Topic = "messages.new",
Title = $"{sender.Nick ?? sender.Account.Nick ?? "Unknown"} ({roomSubject})",
Content = !string.IsNullOrEmpty(message.Content)
? message.Content[..Math.Min(message.Content.Length, 100)]
: "<attachments>",
Meta = new Dictionary<string, object>
{
["message_id"] = message.Id,
["room_id"] = room.Id,
["images"] = message.Attachments
.Where(a => a.MimeType != null && a.MimeType.StartsWith("image"))
.Select(a => a.Id).ToList()
},
: "<no content>",
Meta = metaDict,
Priority = 10,
};
@ -96,7 +106,7 @@ public class ChatService(
Type = type,
Data = message
});
// Only add accounts that aren't null
if (member.Account.Id != sender.AccountId)
accountsToNotify.Add(member.Account);