【问题标题】:How to detect windows 10 toast notification triggered by another app using a UWP app如何使用 UWP 应用检测由另一个应用触发的 Windows 10 Toast 通知
【发布时间】:2021-11-27 07:36:52
【问题描述】:

所以我正在尝试创建一个 UWP 应用程序,它应该能够检测当前是否显示任何 Toast 通知,然后在出现时播放音频文件。我正在尝试使用 GetNotificationsAsync 方法来实现这一点,但问题是无论当前是否显示 toast 通知,该方法生成的列表总是相同的。以下是按钮中将触发该过程的代码 sn-p,是否需要执行一些额外的步骤?此外,已授予应用程序在 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;
                }


            }
            
        }

【问题讨论】:

  • 我尝试了您的代码,但结果表明该代码运行良好。我在输出窗口中打印b 的值,每次单击操作中心的通知时,我都可以看到该值正在发生变化。什么行为让你觉得生成的列表总是一样的?
  • 这很奇怪,对我来说,b 的值总是为零。我需要执行任何其他步骤吗?当我说其他步骤时,我的意思是我只是安装了 Visual Studio 2019,选择了 UWP 模板并输入了此代码。我还需要做些什么才能让代码检测到通知吗?
  • 您的测试设备的操作中心是否还有其他通知?当操作中心有通知时,它不应该总是返回零。
  • 所以代码在不同的机器上工作。似乎问题出在测试设备本身,因为它在两天前崩溃并且不得不在其上重新安装 Windows。现在代码似乎可以工作了。

标签: c# windows visual-studio uwp toast


【解决方案1】:

更新上述内容。 该代码似乎在不同的设备中正常工作。编写此代码的测试机器最终崩溃,必须重新安装 Windows,之后相同的代码现在可以工作。所以看起来安装本身而不是代码有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 2016-09-30
    • 2017-03-19
    • 1970-01-01
    • 2017-02-06
    • 2022-11-25
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多