简体   繁体   中英

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. 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. 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.

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. So looks like there was some problem with the installation itself and not the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM