🔊 Add logs to notification pushing

This commit is contained in:
LittleSheep 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);

View File

@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAccessToken_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FSourcesCache_003Fb370f448e9f5fca62da785172d83a214319335e27ac4d51840349c6dce15d68_003FAccessToken_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AApnSender_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6aadc2cf048f477d8636fb2def7b73648200_003Fc5_003F2a1973a9_003FApnSender_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AApnSettings_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6aadc2cf048f477d8636fb2def7b73648200_003F0f_003F51443844_003FApnSettings_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAuthenticationMiddleware_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe49de78932194d52a02b07486c6d023a24600_003F2f_003F7ab1cc57_003FAuthenticationMiddleware_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAuthenticationSchemeOptions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe49de78932194d52a02b07486c6d023a24600_003Ff0_003F595b6eda_003FAuthenticationSchemeOptions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAuthorizationAppBuilderExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F2ff26593f91746d7a53418a46dc419d1f200_003F4b_003F56550da2_003FAuthorizationAppBuilderExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>