9

My Python test script causes our product to raise Windows notifications ("Toasts"). How can my python script verify that the notifications are indeed raised?

I see it's possible to make a notification listener in C# using Windows.UI.Notifications.Management.UserNotificationListener (ref), And I see I can make my own notifications in Python using win10toast - but how do I listen to othe apps' notifications?

2 Answers 2

3

You can use pywinrt to access the bindings in python. A basic example would look something like this:

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

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 = await listener.request_access_async()

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)   
3
  • I don't work on the same product anymore, so I can't verify the answer. It does sound like the right solution, so accepted, thanks!
    – Jonathan
    Commented Mar 23, 2022 at 8:09
  • listener.add_notification_changed(handler) worked for me
    – Luca
    Commented Sep 8, 2022 at 6:39
  • @Luca Great, I've updated the post accordingly. Commented Sep 8, 2022 at 10:07
1

Searching python windows notification listener on google brings up only this ok-ish result but it is not complete.

Since i couldn't find any self contained example on how to do it, here is a fully working code:

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

def handler(asd, aasd):
    unotification = asd.get_notification(aasd.user_notification_id)
    # print(dir(unotification))
    if hasattr(unotification, "app_info"):
        print("App Name: ", unotification.app_info.display_info.display_name)
        text_sequence = unotification.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            
    else:
        pass

listener = UserNotificationListener.get_current()
listener.add_notification_changed(handler)

while True: pass

tested on windows 10 and winrt v1.0.21033.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.