Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag and group support for notification, add remove and clear function #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions DesktopToast/ToastManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -15,12 +15,42 @@ namespace DesktopToast
/// </summary>
public class ToastManager
{
/// <summary>
/// clear toast notification for appId
/// </summary>
/// <param name="appId">Toast appId</param>
public static void Clear(string appId)
{
try
{
ToastNotificationManager.History.Clear(appId);
}
catch (Exception ex)
{
}
}
/// <summary>
/// remove a toast.
/// </summary>
/// <param name="tag">Toast tag</param>
/// <param name="group">Toast group</param>
/// <param name="appId">Toast appId</param>
public static void Remove(string tag, string group, string appId)
{
try
{
ToastNotificationManager.History.Remove(tag, group, appId);
}
catch (Exception ex)
{
}
}
/// <summary>
/// Shows a toast.
/// </summary>
/// <param name="request">Toast request</param>
/// <returns>Result of showing a toast</returns>
public static async Task<ToastResult> ShowAsync(ToastRequest request)
public static async Task<ToastResult> ShowAsync(ToastRequest request, string tag=null, string group=null)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
Expand All @@ -38,7 +68,7 @@ public static async Task<ToastResult> ShowAsync(ToastRequest request)
if (document == null)
return ToastResult.Invalid;

return await ShowBaseAsync(document, request.AppId);
return await ShowBaseAsync(document, request.AppId, tag, group);
}

/// <summary>
Expand Down Expand Up @@ -78,7 +108,7 @@ public static async Task<ToastResult> ShowAsync(XmlDocument document, string app
if (!OsVersion.IsEightOrNewer)
return ToastResult.Unavailable;

return await ShowBaseAsync(document, appId);
return await ShowBaseAsync(document, appId,null, null);
}

#region Document
Expand Down Expand Up @@ -332,7 +362,7 @@ private static async Task CheckInstallShortcut(ToastRequest request)
/// <param name="document">Toast document</param>
/// <param name="appId">AppUserModelID</param>
/// <returns>Result of showing a toast</returns>
private static async Task<ToastResult> ShowBaseAsync(XmlDocument document, string appId)
private static async Task<ToastResult> ShowBaseAsync(XmlDocument document, string appId, string tag, string group)
{
// Create a toast and prepare to handle toast events.
var toast = new ToastNotification(document);
Expand Down Expand Up @@ -366,10 +396,13 @@ private static async Task<ToastResult> ShowBaseAsync(XmlDocument document, strin
tcs.SetResult(ToastResult.Failed);
};
toast.Failed += failed;
if (!string.IsNullOrEmpty(tag))
toast.Tag = tag;
if (!string.IsNullOrEmpty(group))
toast.Group = group;

// Show a toast.
ToastNotificationManager.CreateToastNotifier(appId).Show(toast);

// Wait for the result.
var result = await tcs.Task;

Expand All @@ -384,4 +417,4 @@ private static async Task<ToastResult> ShowBaseAsync(XmlDocument document, strin

#endregion
}
}
}