[英]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.