diff --git a/DesktopNotifications.Apple/AppleNotificationManager.cs b/DesktopNotifications.Apple/AppleNotificationManager.cs index 76c2253..c2dbaeb 100644 --- a/DesktopNotifications.Apple/AppleNotificationManager.cs +++ b/DesktopNotifications.Apple/AppleNotificationManager.cs @@ -14,10 +14,12 @@ namespace DesktopNotifications.Apple public ValueTask Initialize() { + return default; } public ValueTask ShowNotification(Notification notification, DateTimeOffset? expirationTime = null) { + return default; } } } diff --git a/DesktopNotifications.FreeDesktop/FreeDesktopNotificationManager.cs b/DesktopNotifications.FreeDesktop/FreeDesktopNotificationManager.cs index 0792675..297c8e9 100644 --- a/DesktopNotifications.FreeDesktop/FreeDesktopNotificationManager.cs +++ b/DesktopNotifications.FreeDesktop/FreeDesktopNotificationManager.cs @@ -12,10 +12,10 @@ namespace DesktopNotifications.FreeDesktop private const string NotificationsService = "org.freedesktop.Notifications"; private static readonly ObjectPath NotificationsPath = new ObjectPath("/org/freedesktop/Notifications"); + private readonly Dictionary _activeNotifications; private Connection? _connection; private IDisposable? _notificationActionSubscription; private IDisposable? _notificationCloseSubscription; - private Dictionary _activeNotifications; private IFreeDesktopNotificationsProxy? _proxy; @@ -68,8 +68,8 @@ namespace DesktopNotifications.FreeDesktop "MyApp", 0, string.Empty, - notification.Title, - notification.Body, + notification.Title ?? throw new ArgumentException(), + notification.Body ?? throw new ArgumentException(), actions.ToArray(), new Dictionary {{"urgency", 1}}, duration?.Milliseconds ?? 0 @@ -110,7 +110,7 @@ namespace DesktopNotifications.FreeDesktop var dismissReason = GetReason(@event.reason); - NotificationDismissed?.Invoke(this, + NotificationDismissed?.Invoke(this, new NotificationDismissedEventArgs(notification, dismissReason)); } @@ -123,7 +123,7 @@ namespace DesktopNotifications.FreeDesktop { var notification = _activeNotifications[@event.id]; - NotificationActivated?.Invoke(this, + NotificationActivated?.Invoke(this, new NotificationActivatedEventArgs(notification, @event.actionKey)); } } diff --git a/DesktopNotifications.Windows/WindowsNotificationManager.cs b/DesktopNotifications.Windows/WindowsNotificationManager.cs index 8d7d377..66169bd 100644 --- a/DesktopNotifications.Windows/WindowsNotificationManager.cs +++ b/DesktopNotifications.Windows/WindowsNotificationManager.cs @@ -12,7 +12,9 @@ namespace DesktopNotifications.Windows { private readonly Dictionary _notifications; private readonly ToastNotifier _toastNotifier; + private string? _launchAction; private TaskCompletionSource? _launchActionPromise; + private EventHandler? _notificationActivatedHandler; /// /// @@ -23,7 +25,16 @@ namespace DesktopNotifications.Windows _notifications = new Dictionary(); } - public event EventHandler? NotificationActivated; + public event EventHandler? NotificationActivated + { + add + { + _notificationActivatedHandler += value; + ProcessLaunchAction(); + } + remove => _notificationActivatedHandler -= value; + } + public event EventHandler? NotificationDismissed; public async ValueTask Initialize() @@ -33,31 +44,14 @@ namespace DesktopNotifications.Windows _launchActionPromise = new TaskCompletionSource(); ToastNotificationManagerCompat.OnActivated += OnAppActivated; - var launchAction = await _launchActionPromise.Task; + _launchAction = await _launchActionPromise.Task; - Debug.Assert(launchAction != null); + Debug.Assert(_launchAction != null); - //TODO: Lookup notification object from history? - NotificationActivated?.Invoke(this, - new NotificationActivatedEventArgs(null, launchAction)); + ProcessLaunchAction(); } } - private static XmlDocument GenerateXml(Notification notification) - { - var builder = new ToastContentBuilder(); - - builder.AddText(notification.Title); - builder.AddText(notification.Body); - - foreach (var (title, actionId) in notification.Buttons) - { - builder.AddButton(title, ToastActivationType.Foreground, actionId); - } - - return builder.GetXml(); - } - public ValueTask ShowNotification(Notification notification, DateTimeOffset? expirationTime) { if (expirationTime < DateTimeOffset.Now) @@ -81,6 +75,39 @@ namespace DesktopNotifications.Windows return default; } + public void Dispose() + { + } + + private void ProcessLaunchAction() + { + if (_launchAction == null || _notificationActivatedHandler == null) + { + return; + } + + //TODO: Lookup notification object from history? + _notificationActivatedHandler.Invoke(this, + new NotificationActivatedEventArgs(null, _launchAction)); + + _launchAction = null; + } + + private static XmlDocument GenerateXml(Notification notification) + { + var builder = new ToastContentBuilder(); + + builder.AddText(notification.Title); + builder.AddText(notification.Body); + + foreach (var (title, actionId) in notification.Buttons) + { + builder.AddButton(title, ToastActivationType.Foreground, actionId); + } + + return builder.GetXml(); + } + private void OnAppActivated(ToastNotificationActivatedEventArgsCompat e) { Debug.Assert(_launchActionPromise != null); @@ -114,11 +141,7 @@ namespace DesktopNotifications.Windows var notification = _notifications[sender]; var actionId = string.IsNullOrEmpty(activationArgs.Arguments) ? "default" : activationArgs.Arguments; - NotificationActivated?.Invoke(this, new NotificationActivatedEventArgs(notification, actionId)); - } - - public void Dispose() - { + _notificationActivatedHandler?.Invoke(this, new NotificationActivatedEventArgs(notification, actionId)); } } } \ No newline at end of file diff --git a/DesktopNotifications/Notification.cs b/DesktopNotifications/Notification.cs index ab41fdd..caef52e 100644 --- a/DesktopNotifications/Notification.cs +++ b/DesktopNotifications/Notification.cs @@ -11,9 +11,9 @@ namespace DesktopNotifications Buttons = new List<(string Title, string ActionId)>(); } - public string Title { get; set; } + public string? Title { get; set; } - public string Body { get; set; } + public string? Body { get; set; } public List<(string Title, string ActionId)> Buttons { get; } } diff --git a/Example/Program.cs b/Example/Program.cs index 89f9dbe..9417de6 100644 --- a/Example/Program.cs +++ b/Example/Program.cs @@ -51,12 +51,11 @@ namespace Example private static async Task Main(string[] args) { using var manager = CreateManager(); + await manager.Initialize(); manager.NotificationActivated += ManagerOnNotificationActivated; manager.NotificationDismissed += ManagerOnNotificationDismissed; - await manager.Initialize(); - var notification = new Notification { Title = "Hello World!",