estara/Estara/Services/AppNotificationService.cs

72 lines
1.9 KiB
C#

using System.Collections.Specialized;
using System.Web;
using Estara.Contracts.Services;
using Estara.ViewModels;
using Microsoft.Windows.AppNotifications;
namespace Estara.Notifications;
public class AppNotificationService : IAppNotificationService
{
private readonly INavigationService _navigationService;
public AppNotificationService(INavigationService navigationService)
{
_navigationService = navigationService;
}
~AppNotificationService()
{
Unregister();
}
public void Initialize()
{
AppNotificationManager.Default.NotificationInvoked += OnNotificationInvoked;
AppNotificationManager.Default.Register();
}
public void OnNotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args)
{
// TODO: Handle notification invocations when your app is already running.
//// // Navigate to a specific page based on the notification arguments.
//// if (ParseArguments(args.Argument)["action"] == "Settings")
//// {
//// App.MainWindow.DispatcherQueue.TryEnqueue(() =>
//// {
//// _navigationService.NavigateTo(typeof(SettingsViewModel).FullName!);
//// });
//// }
App.MainWindow.DispatcherQueue.TryEnqueue(() =>
{
App.MainWindow.ShowMessageDialogAsync("TODO: Handle notification invocations when your app is already running.", "Notification Invoked");
App.MainWindow.BringToFront();
});
}
public bool Show(string payload)
{
var appNotification = new AppNotification(payload);
AppNotificationManager.Default.Show(appNotification);
return appNotification.Id != 0;
}
public NameValueCollection ParseArguments(string arguments)
{
return HttpUtility.ParseQueryString(arguments);
}
public void Unregister()
{
AppNotificationManager.Default.Unregister();
}
}