![](/img/trans.png)
[英]How can i can send windows 10 notifications with python that has a button on the notification
[英]How can I listen to Windows 10 notifications in Python?
我的 Python 测试脚本导致我们的产品发出 Windows 通知(“祝酒词”)。 我的 python 脚本如何验证确实发出了通知?
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?
您可以使用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()
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)
在 google 上搜索python windows notification listener
器只会显示这个正常的结果,但它并不完整。
由于我找不到任何关于如何做到这一点的自包含示例,所以这里有一个完整的工作代码:
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
在 windows 10 和 winrt v1.0.21033.1 上测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.