Files
VDownload/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/NotificationsService.cs

79 lines
1.8 KiB
C#
Raw Normal View History

2024-03-10 23:04:39 +01:00
using Microsoft.Toolkit.Uwp.Notifications;
2024-03-14 16:31:26 +01:00
using Microsoft.UI.Xaml;
2024-03-10 23:04:39 +01:00
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
2024-03-14 16:31:26 +01:00
using SimpleToolkit.UI.WinUI.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-03-14 16:31:26 +01:00
using VDownload.Services.Common;
namespace VDownload.Services.UI.Notifications
{
2024-03-14 16:31:26 +01:00
public interface INotificationsService : IInitializableService<Window>
{
void SendNotification(string title, IEnumerable<string> message);
}
public class NotificationsService : INotificationsService
{
2024-03-14 16:31:26 +01:00
#region FIELDS
protected Window _window;
#endregion
#region CONSTRUCTORS
2024-03-10 23:04:39 +01:00
~NotificationsService()
{
AppNotificationManager.Default.Unregister();
}
#endregion
#region PUBLIC METHODS
2024-03-14 16:31:26 +01:00
public async Task Initialize(Window window) => await Task.Run(() =>
2024-03-10 23:04:39 +01:00
{
2024-03-14 16:31:26 +01:00
_window = window;
AppNotificationManager.Default.NotificationInvoked += NotificationInvoked;
2024-03-10 23:04:39 +01:00
AppNotificationManager.Default.Register();
2024-03-14 16:31:26 +01:00
});
2024-03-10 23:04:39 +01:00
public void SendNotification(string title, IEnumerable<string> message)
{
AppNotificationBuilder builder = new AppNotificationBuilder();
builder.AddText(title);
foreach (string text in message)
{
builder.AddText(text);
}
2024-03-10 23:04:39 +01:00
AppNotification notification = builder.BuildNotification();
2024-03-10 23:04:39 +01:00
AppNotificationManager.Default.Show(notification);
}
#endregion
2024-03-14 16:31:26 +01:00
#region PRIVATE METHODS
private void NotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args) => WindowHelper.ShowWindow(_window);
#endregion
}
}