Fix some warnings, Fix Windows launch action notifications

This commit is contained in:
Luis von der Eltz
2021-02-06 14:11:23 +01:00
parent 787ec5e9bf
commit 97a3efc2a6
5 changed files with 59 additions and 35 deletions
@@ -14,10 +14,12 @@ namespace DesktopNotifications.Apple
public ValueTask Initialize()
{
return default;
}
public ValueTask ShowNotification(Notification notification, DateTimeOffset? expirationTime = null)
{
return default;
}
}
}
@@ -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<uint, Notification> _activeNotifications;
private Connection? _connection;
private IDisposable? _notificationActionSubscription;
private IDisposable? _notificationCloseSubscription;
private Dictionary<uint, Notification> _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<string, object> {{"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));
}
}
@@ -12,7 +12,9 @@ namespace DesktopNotifications.Windows
{
private readonly Dictionary<ToastNotification, Notification> _notifications;
private readonly ToastNotifier _toastNotifier;
private string? _launchAction;
private TaskCompletionSource<string?>? _launchActionPromise;
private EventHandler<NotificationActivatedEventArgs>? _notificationActivatedHandler;
/// <summary>
/// </summary>
@@ -23,7 +25,16 @@ namespace DesktopNotifications.Windows
_notifications = new Dictionary<ToastNotification, Notification>();
}
public event EventHandler<NotificationActivatedEventArgs>? NotificationActivated;
public event EventHandler<NotificationActivatedEventArgs>? NotificationActivated
{
add
{
_notificationActivatedHandler += value;
ProcessLaunchAction();
}
remove => _notificationActivatedHandler -= value;
}
public event EventHandler<NotificationDismissedEventArgs>? NotificationDismissed;
public async ValueTask Initialize()
@@ -33,31 +44,14 @@ namespace DesktopNotifications.Windows
_launchActionPromise = new TaskCompletionSource<string?>();
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));
}
}
}
+2 -2
View File
@@ -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; }
}
+1 -2
View File
@@ -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!",