如何使用 UWP 应用程序检测另一个应用程序触发的 windows 10 toast 通知
How to detect windows 10 toast notification triggered by another app using a UWP app
所以我正在尝试创建一个 UWP 应用程序,它应该能够检测当前是否显示任何 toast 通知,然后在显示时播放音频文件。我正在尝试使用 GetNotificationsAsync 方法来实现此目的,但问题是无论当前是否正在显示 toast 通知,该方法生成的列表始终相同。以下是将触发该过程的按钮中的代码片段,是否需要执行一些额外的步骤?此外,已授予该应用程序在 windows 10.
上通知的权限
private async void Button_Click(object sender, RoutedEventArgs e)
{
int a = 0;
switchStatus = "ON";
this.Status.Text = switchStatus;
while (a == 0)
{
// Get the listener
Windows.UI.Notifications.Management.UserNotificationListener listener = Windows.UI.Notifications.Management.UserNotificationListener.Current;
// And request access to the user's notifications (must be called from UI thread)
Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();
switch (accessStatus)
{
// This means the user has granted access.
case Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus.Allowed:
// Get the toast notifications
IReadOnlyList<Windows.UI.Notifications.UserNotification> notifs = await listener.GetNotificationsAsync(Windows.UI.Notifications.NotificationKinds.Toast);
int b = notifs.Count();
if (b==0)
{
break;
}
else
{
try
{
mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/audio.mp3"));
mediaPlayer.AutoPlay = false;
mediaPlayer.Play();
playing = true;
a = 1;
}
catch (Exception)
{
}
}
break;
// This means the user has denied access.
// Any further calls to RequestAccessAsync will instantly
// return Denied. The user must go to the Windows settings
// and manually allow access.
case Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus.Denied:
// Show UI explaining that listener features will not
// work until user allows access.
break;
// This means the user closed the prompt without
// selecting either allow or deny. Further calls to
// RequestAccessAsync will show the dialog again.
case Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus.Unspecified:
// Show UI that allows the user to bring up the prompt again
break;
}
}
}
更新以上内容。
该代码似乎在不同的设备中正常工作。编写此代码的测试机最终崩溃,必须重新安装 windows,之后相同的代码现在可以正常工作。所以看起来安装本身有问题,而不是代码。
所以我正在尝试创建一个 UWP 应用程序,它应该能够检测当前是否显示任何 toast 通知,然后在显示时播放音频文件。我正在尝试使用 GetNotificationsAsync 方法来实现此目的,但问题是无论当前是否正在显示 toast 通知,该方法生成的列表始终相同。以下是将触发该过程的按钮中的代码片段,是否需要执行一些额外的步骤?此外,已授予该应用程序在 windows 10.
上通知的权限private async void Button_Click(object sender, RoutedEventArgs e)
{
int a = 0;
switchStatus = "ON";
this.Status.Text = switchStatus;
while (a == 0)
{
// Get the listener
Windows.UI.Notifications.Management.UserNotificationListener listener = Windows.UI.Notifications.Management.UserNotificationListener.Current;
// And request access to the user's notifications (must be called from UI thread)
Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();
switch (accessStatus)
{
// This means the user has granted access.
case Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus.Allowed:
// Get the toast notifications
IReadOnlyList<Windows.UI.Notifications.UserNotification> notifs = await listener.GetNotificationsAsync(Windows.UI.Notifications.NotificationKinds.Toast);
int b = notifs.Count();
if (b==0)
{
break;
}
else
{
try
{
mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/audio.mp3"));
mediaPlayer.AutoPlay = false;
mediaPlayer.Play();
playing = true;
a = 1;
}
catch (Exception)
{
}
}
break;
// This means the user has denied access.
// Any further calls to RequestAccessAsync will instantly
// return Denied. The user must go to the Windows settings
// and manually allow access.
case Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus.Denied:
// Show UI explaining that listener features will not
// work until user allows access.
break;
// This means the user closed the prompt without
// selecting either allow or deny. Further calls to
// RequestAccessAsync will show the dialog again.
case Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus.Unspecified:
// Show UI that allows the user to bring up the prompt again
break;
}
}
}
更新以上内容。 该代码似乎在不同的设备中正常工作。编写此代码的测试机最终崩溃,必须重新安装 windows,之后相同的代码现在可以正常工作。所以看起来安装本身有问题,而不是代码。