Skip to content

Instantly share code, notes, and snippets.

@GitHub30
Forked from MarcAlx/notification.py
Last active August 7, 2022 08:32
Show Gist options
  • Save GitHub30/4762878d20de3ecbf6657fed23a0734d to your computer and use it in GitHub Desktop.
Save GitHub30/4762878d20de3ecbf6657fed23a0734d to your computer and use it in GitHub Desktop.
Send Windows notifications from python3
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)
# -*- 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))
@GitHub30
Copy link
Author

GitHub30 commented Aug 7, 2022

エディション Windows 10 Enterprise Evaluation
バージョン 21H1
インストール日 ‎6/‎19/‎2021
OS ビルド 19043.1826
エクスペリエンス Windows Feature Experience Pack 120.2212.4180.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment