Отправка уведомления от имени другого приложения

В продолжение этого поста
Когда у вас AOT приложение, которое запускается от администратора - 99% библиотек для работы с COM не работают, плюс для Windows App SDK отдельно указано "Notifications for an elevated (admin) app is currently not supported.", а issue на github висит с 2023 года и поэтому для отправки системных уведомлений надо выкручиваться через другое приложение/PowerShell.
Пример как отправлять уведомления:
using System.Diagnostics;
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications;
using Vanara.PInvoke;
using Vanara.Windows.Shell;
namespace ConsoleNotifications;
class Program
{
static async Task Main(string[] args)
{
string messageTitle = "Habr";
string messageText = "Hello Habrahabr!";
string targetExePath = Environment.SystemDirectory + "\\notepad.exe";
string appUserModelId = GetAppAumid().First(var=>var.Name.Contains("Chrome")).Aumid;
await SendNotification(appUserModelId, messageTitle, messageText, targetExePath);
}
private static Task SendNotification(string appUserModelId, string title, string content, string targetPath)
{
var tcs = new TaskCompletionSource();
var notification = new ToastNotification(new ToastContentBuilder().AddText(title)
.AddText(content).Content.GetXml());
notification.Priority = ToastNotificationPriority.High;
notification.Activated += (s, e) =>
{
Process.Start(new ProcessStartInfo(targetPath));
tcs.SetResult();
};
notification.Failed += (s, e) => tcs.SetResult();
notification.Dismissed += (s, e) => tcs.SetResult();
ToastNotificationManager.CreateToastNotifier(appUserModelId).Show(notification);
return tcs.Task;
}
public sealed record AppAumidInfo(string Name, string Aumid);
public static List<AppAumidInfo> GetAppAumids()
{
var results = new List<AppAumidInfo>();
var f = new ShellFolder(Shell32.KNOWNFOLDERID.FOLDERID_AppsFolder);
foreach (ShellItem i in f.EnumerateChildren(FolderItemFilter.NonFolders | FolderItemFilter.Folders))
results.Add(new AppAumidInfo(i.Name, i.ParsingName));
return results;
}
}В вызываемом приложении надо выставить AppUserModelID и предварительно создать ссылку на него
[LibraryImport("shell32.dll", SetLastError=true)]
internal static partial void SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID);Создание .lnk ссылки с параметрами:
var link = new ShellLink()
{
ToastActivatorId = guid,
AppUserModelID = appUserModelId,
TargetPath = exePath
};
link.Save(shortcutPath);P.S. Буду рад услышать про другие существующие решения этой проблемы











