0

I'm trying to create a script where it is able to listen to windows notifications and found this website "How can i listen to windows 10 notification" I tried to use the guide from that website but unfortunately received an "Element not found" error. I was hoping if someone could explain to me how this error occur and the methods to fix it.

Below is the code that I used. (mostly from the website stated above)

from winrt.windows.ui.notifications.management import UserNotificationListener, UserNotificationListenerAccessStatus
from winrt.windows.foundation.metadata import ApiInformation

if not ApiInformation.is_type_present("Windows.UI.Notifications.Management.UserNotificationListener"):
    print("UserNotificationListener is not supported on this device.")
    exit()

listener = UserNotificationListener.get_current()
accessStatus = listener.get_access_status()

if accessStatus != UserNotificationListenerAccessStatus.ALLOWED:
    print("Access to UserNotificationListener is not allowed.")
    exit()

def handler(listener, event):
    notification = listener.get_notification(event.user_notification_id)

    # get some app info if available
    if hasattr(notification, "app_info"):
        print("App Name: ", notification.app_info.display_info.display_name)

listener.add_notification_changed(handler)

Error I received:

listener.add_notification_changed(handler)
RuntimeError: Element not found.
Derpy
  • 3
  • 5

1 Answers1

0

I also ran into the same issue with "How can i listen to windows 10 notification"

I cobbled together something as a workaround based on this post: Stack Overflow The LED. It takes all the notifications and prints them out.

from winrt.windows.ui.notifications.management import UserNotificationListener
from winrt.windows.ui.notifications import NotificationKinds, KnownNotificationBindings

listener = UserNotificationListener.get_current()
notifications = await listener.get_notifications_async(NotificationKinds.TOAST)

for i in notifications:
    text_sequence = i.notification.visual.get_binding(KnownNotificationBindings.get_toast_generic()).get_text_elements()
    it = iter(text_sequence)
    print("Notification title: ", it.current.text)
    while True:
        next(it, None)
        if it.has_current:
            print(it.current.text)
        else:
            break 

As for the "RuntimeError: Element not found." error perhaps the resolution to this issue is a hint to the solution, but I can't figure out how to apply it to this case.