diff --git a/source/INotificationService.cs b/source/INotificationService.cs index 289820d..b10ddc5 100644 --- a/source/INotificationService.cs +++ b/source/INotificationService.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using Microsoft.JSInterop; +using System.Threading.Tasks; namespace Append.Blazor.Notifications { @@ -26,15 +27,15 @@ public interface INotificationService /// /// /// - /// - ValueTask CreateAsync(string title, NotificationOptions options); + /// The created . + ValueTask CreateAsync(string title, NotificationOptions options); /// /// Creates a Notifcation with the supplied parameters. /// /// The title of the notification. /// The body or description of the notification. /// Link to a image, can be remote or served from the filesystem. - /// - ValueTask CreateAsync(string title, string description, string iconUrl = null); + /// The created . + ValueTask CreateAsync(string title, string description, string iconUrl = null); } } diff --git a/source/Notification.cs b/source/Notification.cs index cbdbb15..3dd96d1 100644 --- a/source/Notification.cs +++ b/source/Notification.cs @@ -1,16 +1,19 @@ -using System; +using Microsoft.JSInterop; +using System; +using System.Threading.Tasks; namespace Append.Blazor.Notifications { /// /// A notification is an abstract representation of something that happened, such as the delivery of a message. /// - public class Notification + public class Notification : IAsyncDisposable { + public Guid Id { get; set; } = Guid.NewGuid(); + /// /// Defines a title for the notification, which will be shown at the top of the notification window when it is fired. /// - public Guid Id { get; set; } = Guid.NewGuid(); public string Title { get; private set; } /// @@ -75,10 +78,14 @@ public class Notification /// public int TimeOut { get; private set; } = 5; - public Notification(string title, NotificationOptions options = null) + private readonly IJSObjectReference _jsObject; + + internal Notification(string title, IJSObjectReference jsObject, NotificationOptions options = null) { if (string.IsNullOrWhiteSpace(title)) throw new ArgumentNullException($"{nameof(title)}, cannot be null or empty."); + if (jsObject is null) + throw new ArgumentNullException(nameof(jsObject)); Title = title; @@ -101,6 +108,22 @@ public Notification(string title, NotificationOptions options = null) NoScreen = options.NoScreen; Sticky = options.Sticky; TimeOut = options.TimeOut ?? TimeOut; + + _jsObject = jsObject; + } + + /// + /// Dismiss the notification. + /// + /// + public ValueTask Close() + { + return _jsObject.InvokeVoidAsync("close"); + } + + public async ValueTask DisposeAsync() + { + await _jsObject.DisposeAsync(); } } } diff --git a/source/NotificationService.cs b/source/NotificationService.cs index 9988af0..d1ef6ca 100644 --- a/source/NotificationService.cs +++ b/source/NotificationService.cs @@ -37,14 +37,15 @@ public async ValueTask RequestPermissionAsync() } /// - public async ValueTask CreateAsync(string title, NotificationOptions options) + public async ValueTask CreateAsync(string title, NotificationOptions options) { var module = await _moduleTask.Value; - await module.InvokeVoidAsync("create", title, options); + var jsObject = await module.InvokeAsync("create", title, options); + return new Notification(title, jsObject, options); } /// - public async ValueTask CreateAsync(string title, string body, string icon = null) + public async ValueTask CreateAsync(string title, string body, string icon = null) { var module = await _moduleTask.Value; @@ -54,7 +55,8 @@ public async ValueTask CreateAsync(string title, string body, string icon = null Icon = icon, }; - await module.InvokeVoidAsync("create", title, options); + var jsObject = await module.InvokeAsync("create", title, options); + return new Notification(title, jsObject, options); } public async ValueTask DisposeAsync()