一、实现说明
通过 AUMID 来清理指定的 Toast 通知。AUMID 是 UWP 或支持通知的 Win32 应用程序的注册标志,可以通过应用程序的 manifest 文件定义。AUMID 格式为 PackageFamilyName!ApplicationId
AUMID 的查找方式:
1、通过注册表 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Notifications\Settings
二、功能示例
点击按钮,触发功能。 先检测是否有该 AUMID 的通知历史,如果有就清除。
C#实现:
using System.Windows;
using Windows.UI.Notifications;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
static void ClearNotificationsByAUMID(string aumid)
{
try
{
// 清除指定 AUMID 的通知历史
ToastNotificationManager.History.Clear(aumid);
Console.WriteLine($"成功清除 AUMID 为 {aumid} 的应用通知。");
}
catch (Exception ex)
{
Console.WriteLine($"错误: {ex.Message}");
}
}
static bool CheckNotificationsByAUMID(string aumid)
{
try
{
// 获取指定 AUMID 的通知历史列表
IReadOnlyList<ToastNotification> notifications = ToastNotificationManager.History.GetHistory(aumid);
bool hasNotifications = notifications.Count > 0;
Console.WriteLine($"AUMID 为 {aumid} 的应用 {(hasNotifications ? "存在" : "不存在")}通知。");
return hasNotifications;
}
catch (Exception ex)
{
Console.WriteLine($"查询错误: {ex.Message}");
return false;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string targetAUMID = "SuzhouSuiwenAITechnologie.mfpush_t2s4wrr8r6eqt!App";
// 获取当前时间
DateTime endTime = DateTime.Now.AddMinutes(1); // 设定结束时间为当前时间加一分钟
// 触发后间隔 3 秒,检测 1 分钟后退出
while (DateTime.Now < endTime)
{
bool hasNotifications = CheckNotificationsByAUMID(targetAUMID);
// 触发清理
if (hasNotifications)
{
ClearNotificationsByAUMID(targetAUMID);
}
// 等待 3 秒
Thread.Sleep(3000);
}
}
}
}
c++实现:
#include <windows.h>
#include <winrt/Windows.UI.Notifications.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <stdio.h>
using namespace winrt;
using namespace Windows::UI::Notifications;
using namespace Windows::Foundation::Collections;
// 函数声明
BOOL ClearNotificationHistoryWinRT(LPCWSTR& aumid);
BOOL CheckNotificationExistsWinRT(LPCWSTR& aumid);
void MonitorNotificationWinRT(LPCWSTR& targetAUMID);
int main() {
// 初始化WinRT运行时(整个程序只需调用一次)
init_apartment(apartment_type::single_threaded);
LPCWSTR lpAumid = L"SuzhouSuiwenAITechnologie.mfpush_t2s4wrr8r6eqt!App";
MonitorNotificationWinRT(lpAumid);
return 0;
}
void MonitorNotificationWinRT(LPCWSTR& targetAUMID)
{
// Set end time to current time plus one minute (60 seconds)
auto endTime = std::chrono::steady_clock::now() + std::chrono::minutes(1);
while (std::chrono::steady_clock::now() < endTime)
{
bool hasNotifications = CheckNotificationExistsWinRT(targetAUMID);
// Trigger clearing if notifications exist
if (hasNotifications)
{
ClearNotificationHistoryWinRT(targetAUMID);
}
// Wait for 3 seconds before checking again
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
// 检查指定AUMID是否存在通知
BOOL CheckNotificationExistsWinRT(LPCWSTR& targetAUMID) {
try {
// 获取指定AUMID的通知历史
auto notifications = ToastNotificationManager::History().GetHistory(targetAUMID);
// 判断通知列表是否为空
return notifications.Size() > 0;
}
catch (const hresult_error& ex) {
wprintf(L"检查通知失败 (HRESULT: 0x%08X): %ls\n", ex.code(), ex.message().c_str());
return FALSE;
}
}
// 清除指定AUMID的通知历史(已移除重复的初始化)
BOOL ClearNotificationHistoryWinRT(LPCWSTR& targetAUMID) {
try {
ToastNotificationManager::History().Clear(targetAUMID);
return TRUE;
}
catch (const hresult_error& ex) {
wprintf(L"清除通知失败 (HRESULT: 0x%08X): %ls\n", ex.code(), ex.message().c_str());
return FALSE;
}
}