[英]How to detect windows 10 toast notification triggered by another app using a UWP app
So I am trying to create a UWP app, which should be able to detect if there are any toast notifications currently being displayed and then when it does, play an audio file.所以我正在尝试创建一个 UWP 应用程序,它应该能够检测当前是否有任何 Toast 通知正在显示,然后在显示时播放音频文件。 I am trying to use the GetNotificationsAsync method to achieve this, but problem is that list generated by the method is always the same regardless of whether a toast notification is currently being displayed or not.
我正在尝试使用 GetNotificationsAsync 方法来实现这一点,但问题是该方法生成的列表始终相同,无论当前是否显示 Toast 通知。 Following is the code snippet within the button that will trigger the process, is there some additional step to be performed?
以下是将触发该过程的按钮中的代码片段,是否需要执行一些额外的步骤? Also Permission has been granted to the app for notifications on windows 10.
此外,该应用程序还获得了在 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;
}
}
}
Update on the above.更新上述内容。 The code appears to be working correctly in a different device.
该代码似乎在不同的设备上正常工作。 The test machine, on which this code was written, eventually crashed and windows had to be reinstalled, following which the same code is now working.
编写此代码的测试机器最终崩溃了,必须重新安装 Windows,之后相同的代码现在可以运行了。 So looks like there was some problem with the installation itself and not the code.
所以看起来安装本身存在一些问题,而不是代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.