C# .NET UserNotificationListener element not found

0

I'm trying to hook into the UserNotificationListener in order to intercept Windows 10 Notifications however I'm unable to hook the event itself nor retrieve the notifications directly without the event. I need this to run in a Winforms application and not UWP application but unfortunately the microsoft docs explain how to do this only for UWP. (https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/notification-listener)

private async void checkUserNotificationListenerAccess()
    {
        if (ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener"))
        {
            listener = UserNotificationListener.Current;
            UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();
            try
            {
                listener.NotificationChanged += Listener_NotificationChanged;
                Debug.WriteLine("Listener Hooked");
            }
            catch (Exception e)
            {
                var access_status = UserNotificationListener.Current.GetAccessStatus();
                Debug.WriteLine("Notifications Hook failed");
            }
        }
    }

The Exception always fires giving the error "Element not found. (Exception from HRESULT: 0x80070490)". The variable access_status returns "Allowed".

I have manually added the references System.Runtime.WindowsRuntime and Windows.winmd in order to get it to build successfully and I am running this on Windows 10 Build 19042.789.

Does anybody have any idea on how to sort this out?

EDIT: I've managed to sort this out. I've recreated my project using .NET Core 3.1 as that had an easier process for having the libraries I needed. The main step however that I had to take to solve this problem was to create an MSIX installer for the application (Windows Application Packaging Project within Visual Studio). When the app is installed with the installer configured as per Microsoft instructions link I posted above, it then works.

c#
.net
visual-studio
notifications
toast
asked on Stack Overflow Feb 7, 2021 by Erik9519 • edited Feb 13, 2021 by Erik9519

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0