using Microsoft.Windows.AppNotifications; using Microsoft.Windows.AppNotifications.Builder; using WindowsGotify.Models; namespace WindowsGotify.Services; public sealed class NotificationService { private readonly NotificationConfig _config; private readonly Logger _logger; public NotificationService(AppConfig config, Logger logger) { _config = config.Notification; _logger = logger; } public Task ShowAsync(GotifyMessage message) { if (!_config.Enabled) return Task.CompletedTask; try { var title = string.IsNullOrWhiteSpace(message.Title) ? "Gotify" : message.Title.Trim(); var body = message.Message ?? string.Empty; if (body.Length > _config.MaxMessageLength) { body = body[..Math.Max(0, _config.MaxMessageLength)] + "…"; } var builder = new AppNotificationBuilder() .AddText(title) .AddText(body); if (_config.Sound) { builder.SetAudioEvent( message.Priority >= 8 ? AppNotificationSoundEvent.Alarm : AppNotificationSoundEvent.Default); } // 把 extras 里的 client::notification.click.url 透传给通知,让 App.OnNotificationInvoked 收到 // Windows App SDK 2.4.0 的 AddArgument 是 (key, value) 两参,跟 1.x 的单参 "key=value" 不一样 var clickUrl = message.ClickUrl; if (!string.IsNullOrWhiteSpace(clickUrl)) { builder.AddArgument("url", clickUrl); } var notification = builder.BuildNotification(); AppNotificationManager.Default.Show(notification); _logger.Info($"Notification shown. id={message.Id}, priority={message.Priority}, title={title}"); } catch (Exception ex) { _logger.Error($"Notification display failed. id={message.Id}", ex); } return Task.CompletedTask; } }