Skip to content

Instantly share code, notes, and snippets.

@MarcAlx
Last active January 24, 2023 22:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MarcAlx/443358d5e7167864679ffa1b7d51cd06 to your computer and use it in GitHub Desktop.
Save MarcAlx/443358d5e7167864679ffa1b7d51cd06 to your computer and use it in GitHub Desktop.
Send Windows notifications from python3
# -*- 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 winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier();
#define your notification as string
tString = """
<toast>
<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))
@juun0
Copy link

juun0 commented Aug 23, 2019

what do you mean "create_toast_notifier();"

@MarcAlx
Copy link
Author

MarcAlx commented Aug 23, 2019

Hi, this is linked to one of my StackOverflow's answer. You need to install a library that makes Windows library available in python.

pip install winrt

(Which is documented here)

So "create_toast_notifier();" is the mapping for ToastNotificationManager.CreateToastNotifier as explained there.

I will add requirement comment to this gist.

@AltoRetrato
Copy link

Hi!

When I run your code, I get:

Traceback (most recent call last):
  File "notification.py", line 14, in <module>
    notifier = nManager.create_toast_notifier();
RuntimeError: Element not found.

Any idea of what is the problem?

@MarcAlx
Copy link
Author

MarcAlx commented Apr 11, 2020

Do you match the following requirements ?

Windows 10, October 2018 Update or later.
Python for Windows, version 3.7 or later
pip, version 19 or later

As pointed here : https://github.com/Microsoft/xlang/tree/master/src/package/pywinrt/projection#prerequisites

@AltoRetrato
Copy link

I match all requirements:

Windows 10 Home, version 1909 (OS Build 18363.752)
Python 3.7.0
pip 20.0.2

But when I searched for more information about Python/WinRT, I've found that the xlang team is not actively working on Python/WinRT, and the Python/WinRT project is not currently in a usable state, so at least for now I won't be using Python/WinRT. Thank you for your reply, anyway!

@alexanderbeloiu
Copy link

@AltoRetrato (and future viewers) I solved the error by changingnotifier = nManager.create_toast_notifier(); to notifier = nManager.create_toast_notifier("App-id);
However now no notification is showing.

@iDre4M
Copy link

iDre4M commented Dec 26, 2020

So I've tried this example as the first attempt, but couldn't make it work in any possible way (maybe I'm missing smth). But in the end found sort of OK and clear example to get it running, it worked properly, just couldn't find a way to add image tho... that's for future probably:

from winrt.windows.ui.notifications import ToastNotificationManager, ToastNotification, ToastTemplateType

def toast_notification(AppID, title, text):
    XML = ToastNotificationManager.get_template_content(ToastTemplateType.TOAST_TEXT02)
    t = XML.get_elements_by_tag_name("text")
    t[0].append_child(XML.create_text_node(title))
    t[1].append_child(XML.create_text_node(text))
    notifier = ToastNotificationManager.create_toast_notifier(AppID)
    notifier.show(ToastNotification(XML))`

@MarcAlx
Copy link
Author

MarcAlx commented Jan 5, 2021

@iDre4M cool that you make it work !

The only difference I see is that you provide an AppID, is it a random string or an Id you register elsewhere ?

Also It would be interesting to see the XML you give to show.

@MarcAlx
Copy link
Author

MarcAlx commented Jan 5, 2021

For image you may look at the xml schema description by Microsoft : https://docs.microsoft.com/fr-fr/windows/uwp/design/shell/tiles-and-notifications/toast-schema#adaptiveimage seems to be an url pointing to an image.

@iDre4M
Copy link

iDre4M commented Jan 20, 2021

@iDre4M cool that you make it work !

The only difference I see is that you provide an AppID, is it a random string or an Id you register elsewhere ?

Also It would be interesting to see the XML you give to show.

Well, this attempt is purely C# ish, where you don't write all that XML structure in a string, just add elements (that probably in the background either way ends up in XML structure) and XML was stated in the first line of the function.

Managed to fix issue with XML too, had to remove all the spaces at the start of each line, add "", give all elements their "IDs" and attributes if needed, no images from outside the local disk even then you had to give full path, buttons don't exist, all other functions are kind of out, unless that's because used older version, since latest (at that time) was experiencing import issues.

@N3RDIUM
Copy link

N3RDIUM commented May 24, 2021

How do I get buttons to work?

@GitHub30
Copy link

GitHub30 commented Aug 9, 2022

Hi guys, I've created this as an easy to use package

https://pypi.org/project/win11toast/

@MarcAlx
Copy link
Author

MarcAlx commented Aug 9, 2022

Hi guys, I've created this as an easy to use package

https://pypi.org/project/win11toast/

Wow nice work! happy to have helped you with this gist.

@krn17
Copy link

krn17 commented Jan 24, 2023

is there a way to stack upcoming notifications over the previous ones as they come?

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