简体   繁体   English

如何使用 UWP 应用检测由另一个应用触发的 Windows 10 Toast 通知

[英]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.

相关问题 如何在UWP App中创建信息丰富的Toast通知 - How to create informative toast notification in UWP App WPF桌面应用程序,Windows 10通知Toast 2016(UWP社区工具包) - WPF Desktop App, Windows 10 Notification Toast 2016 (UWP Community Toolkit) 可以从非 UWP C# 应用程序在 Windows 10 中发送 Toast 通知吗? - Possible to send Toast Notification in Windows 10 from a non-UWP C# app? Windows 10中来自桌面应用程序的Toast通知中的意外行为 - Unexpected behaviours in toast notification from desktop app in Windows 10 如何在另一个UWP-App(在Windows 10下)的Webview中显示UWP-App? - How do i display a UWP-App in a Webview in another UWP-App (under Windows 10)? Windows 8.1应用程序中的Toast通知 - Toast notification in windows 8.1 app 如何防止在单击 windows 吐司时启动 UWP 应用程序 - How to prevent launch of UWP app on click of windows toast 如何在Windows 8应用中安排特定日期和时间的祝酒通知? - How to schedule a toast notification for a specific date and time in windows 8 app? Windows Phone 7 Toast通知,前台有应用程序 - Windows phone 7 Toast notification with app in foreground 与另一个组织的员工共享 Windows 10 商店 uwp 应用程序 - share windows 10 store uwp app with employees of another organization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM