-
-
Save GitHub30/4762878d20de3ecbf6657fed23a0734d to your computer and use it in GitHub Desktop.
Send Windows notifications from python3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import winsdk.windows.ui.notifications as notifications | |
import winsdk.windows.data.xml.dom as dom | |
from winsdk.windows.ui.notifications import ToastActivatedEventArgs, ToastDismissedEventArgs, ToastFailedEventArgs | |
# create notifier | |
nManager = notifications.ToastNotificationManager | |
notifier = nManager.create_toast_notifier() | |
# define your notification as string | |
tString = """ | |
<toast activationType="background"> | |
<visual> | |
<binding template='ToastGeneric'> | |
<text>Sample toast2</text> | |
<text>Sample content</text> | |
</binding> | |
</visual> | |
<actions><action content="Button" activationType="protocol" arguments="C:\Windows\Media\Alarm01.wav"/></actions> | |
</toast> | |
""" | |
# https://docs.microsoft.com/en-us/windows/uwp/launch-resume/launch-default-app | |
# http: or file: | |
tString = """ | |
<toast activationType="protocol" launch="http:"> | |
<visual> | |
<binding template='ToastGeneric'> | |
<text>Sample toast23</text> | |
<text>Sample content</text> | |
</binding> | |
</visual> | |
</toast> | |
""" | |
# convert notification to an XmlDocument | |
xDoc = dom.XmlDocument() | |
xDoc.load_xml(tString) | |
notification = notifications.ToastNotification(xDoc) | |
notification.add_activated(lambda x, y: print(x, ToastActivatedEventArgs._from(y).arguments, dir(ToastActivatedEventArgs._from(y)))) | |
notification.add_dismissed(lambda x, y: print(x, dir(x), dir(y), ToastDismissedEventArgs._from(y).reason, dir(ToastDismissedEventArgs._from(y)))) | |
#notification.add_failed(lambda x, y: print('add_failed', x, dir(x), dir(y), ToastFailedEventArgs._from(y).error_code, dir(ToastFailedEventArgs._from(y)))) | |
# display notification | |
notifier.show(notification) | |
import time | |
time.sleep(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Created by Marc_Alx | |
# | |
# pip install winrt | |
# Documentation here : https://github.com/Microsoft/xlang/tree/master/src/package/pywinrt/projection | |
# NB Only works with version of Windows that supports 'windows.ui.notifications' | |
# | |
# Requirements | |
# Windows 10, October 2018 Update or later. | |
# Python for Windows, version 3.7 or later | |
# pip, version 19 or later | |
# | |
import winsdk.windows.ui.notifications as notifications | |
import winsdk.windows.data.xml.dom as dom | |
#create notifier | |
nManager = notifications.ToastNotificationManager | |
notifier = nManager.create_toast_notifier(); | |
#define your notification as string | |
tString = """ | |
<toast activationType="background"> | |
<visual> | |
<binding template='ToastGeneric'> | |
<text>Sample toast</text> | |
<text>Sample content</text> | |
</binding> | |
</visual> | |
</toast> | |
""" | |
#convert notification to an XmlDocument | |
xDoc = dom.XmlDocument() | |
xDoc.load_xml(tString) | |
#display notification | |
notifier.show(notifications.ToastNotification(xDoc)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
エディション Windows 10 Enterprise Evaluation
バージョン 21H1
インストール日 6/19/2021
OS ビルド 19043.1826
エクスペリエンス Windows Feature Experience Pack 120.2212.4180.0