如何在 Python 中收听 Windows 10 条通知?

How can I listen to Windows 10 notifications in Python?

我的 Python 测试脚本导致我们的产品引发 Windows 通知(“Toasts”)。我的 python 脚本如何验证通知确实已发出?

我发现可以使用 Windows.UI.Notifications.Management.UserNotificationListener 在 C# 中创建通知侦听器(ref), And I see I can make my own notifications in Python using win10toast - 但我如何侦听其他应用程序的通知?

您可以使用 pywinrt 将相同的内容归档到 python。 一个基本示例如下所示:

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()

notifications = await listener.get_notifications_async(NotificationKinds.TOAST)
    

订阅 NotificationChanged (listener.add_notification_changed(handler)) 似乎在 python 包中被破坏了。