Use Microsoft Package to register toast notification

This commit is contained in:
Hongtao Zhang 2022-01-06 19:15:50 -06:00
parent b9048f50f0
commit a4dea1f2fd
3 changed files with 16 additions and 8 deletions

View file

@ -88,6 +88,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="InputSimulator" Version="1.0.4" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
<PackageReference Include="ModernWpfUI" Version="0.9.4" />
<PackageReference Include="NHotkey.Wpf" Version="2.1.0" />
<PackageReference Include="NuGet.CommandLine" Version="5.4.0">

View file

@ -59,6 +59,7 @@ namespace Flow.Launcher
_viewModel.Save();
e.Cancel = true;
await PluginManager.DisposePluginsAsync();
Notification.Uninstall();
Environment.Exit(0);
}

View file

@ -1,4 +1,5 @@
using Flow.Launcher.Infrastructure;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.IO;
using Windows.Data.Xml.Dom;
@ -8,10 +9,17 @@ namespace Flow.Launcher
{
internal static class Notification
{
internal static bool legacy = Environment.OSVersion.Version.Build < 19041;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
internal static void Uninstall()
{
if (!legacy)
ToastNotificationManagerCompat.Uninstall();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
public static void Show(string title, string subTitle, string iconPath)
{
var legacy = Environment.OSVersion.Version.Build < 19041;
// Handle notification for win7/8/early win10
if (legacy)
{
@ -24,13 +32,11 @@ namespace Flow.Launcher
? Path.Combine(Constant.ProgramDirectory, "Images\\app.png")
: iconPath;
var xml = $"<?xml version=\"1.0\"?><toast><visual><binding template=\"ToastImageAndText04\"><image id=\"1\" src=\"{Icon}\" alt=\"meziantou\"/><text id=\"1\">{title}</text>" +
$"<text id=\"2\">{subTitle}</text></binding></visual></toast>";
var toastXml = new XmlDocument();
toastXml.LoadXml(xml);
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier("Flow Launcher").Show(toast);
new ToastContentBuilder()
.AddText(title, hintMaxLines: 1)
.AddText(subTitle)
.AddAppLogoOverride(new Uri(Icon))
.Show();
}
private static void LegacyShow(string title, string subTitle, string iconPath)