Add native OS notifications

This commit is contained in:
Stone_Red
2026-07-19 20:40:31 +02:00
parent f1eee397b5
commit c93a53da51
4 changed files with 54 additions and 2 deletions
+6 -1
View File
@@ -69,6 +69,10 @@ public partial class App : Application
NotificationSoundService notificationService = new(ConfigManager.Load().Notifications);
Locator.CurrentMutable.RegisterLazySingleton<NotificationSoundService>(() => notificationService);
OsNotificationService osNotificationService = new();
osNotificationService.Initialize();
Locator.CurrentMutable.RegisterLazySingleton<OsNotificationService>(() => osNotificationService);
Locator.CurrentMutable.Register<MainWindowViewModel>(() =>
new MainWindowViewModel(
Locator.Current.GetService<IConnectionService>()!,
@@ -76,6 +80,7 @@ public partial class App : Application
Locator.Current.GetService<IUserService>()!,
Locator.Current.GetService<IInviteService>()!,
Locator.Current.GetService<CommandHandler>()!,
Locator.Current.GetService<NotificationSoundService>()!));
Locator.Current.GetService<NotificationSoundService>()!,
Locator.Current.GetService<OsNotificationService>()!));
}
}
+1
View File
@@ -25,6 +25,7 @@
</PackageReference>
<PackageReference Include="MessageBox.Avalonia" Version="3.2.0" />
<PackageReference Include="NetCoreAudio" Version="2.0.1" />
<PackageReference Include="OsNotifications" Version="1.1.5" />
<PackageReference Include="Projektanker.Icons.Avalonia.FontAwesome" Version="9.6.2" />
<PackageReference Include="ReactiveUI.Avalonia" Version="11.3.8" />
<PackageReference Include="Romzetron.Avalonia" Version="11.3.4" />
@@ -0,0 +1,41 @@
using OsNotifications;
using Serilog;
namespace Decho.Services;
public sealed class OsNotificationService
{
private bool _initialized;
public void Initialize()
{
if (_initialized) return;
try
{
Notifications.SetGuiApplication(true);
_initialized = true;
}
catch (Exception ex)
{
Log.Warning(ex, "Failed to initialize OS notifications");
}
}
public void Show(string title, string? body = null)
{
if (!_initialized) return;
try
{
Notifications.ShowNotification(title, body ?? string.Empty);
}
catch (PlatformNotSupportedException ex)
{
Log.Warning(ex, "OS notifications not supported on this platform");
}
catch (Exception ex)
{
Log.Warning(ex, "Failed to show OS notification");
}
}
}
+6 -1
View File
@@ -27,6 +27,7 @@ public sealed class MainWindowViewModel : ViewModelBase
{
private readonly CommandHandler _commandHandler;
private readonly NotificationSoundService _notificationService;
private readonly OsNotificationService _osNotificationService;
private Window? _mainWindow;
public string Title { get; } = "Decho";
@@ -45,7 +46,7 @@ public sealed class MainWindowViewModel : ViewModelBase
public ReactiveCommand<Unit, Unit> AddServerCommand { get; }
public MainWindowViewModel(IConnectionService connectionService, IChannelService channelService, IUserService userService, IInviteService inviteService, CommandHandler commandHandler, NotificationSoundService notificationService)
public MainWindowViewModel(IConnectionService connectionService, IChannelService channelService, IUserService userService, IInviteService inviteService, CommandHandler commandHandler, NotificationSoundService notificationService, OsNotificationService osNotificationService)
{
ConnectionService = connectionService;
ChannelService = channelService;
@@ -53,6 +54,7 @@ public sealed class MainWindowViewModel : ViewModelBase
InviteService = inviteService;
_commandHandler = commandHandler;
_notificationService = notificationService;
_osNotificationService = osNotificationService;
Sidebar = new SidebarViewModel();
Chat = new ChatViewModel();
@@ -725,6 +727,9 @@ public sealed class MainWindowViewModel : ViewModelBase
if (isMention)
{
_ = _notificationService.PlayAsync();
string title = $"@{username} {new Uri(serverUrl).Host}";
string body = $"{message.Author.DisplayName} in #{message.ChannelName}: {message.Content}";
_osNotificationService.Show(title, body);
}
};