When i try to make a notification with winrt, it makes an error
Question:
I am trying to make winRT send notifications.
I tried doing this to make a notification:
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))
Hoever, it returns this error, when i try to run it.
notifier = notifications.ToastNotificationManager.create_toast_notifier()
RuntimeError: Element not found.
My system meets the requierments of winrt
Windows 10, October 2018 Update or later.
Python for Windows, version 3.7 or later
pip, version 19 or later
How do i fix this error? I can not use other modules, because winrt is the olny one (that I know of) where you can create ui elements on the notification such as buttons.
Answers:
The exception happens because you need to provide an applicationID
in create_toast_notifier()
Like this for example create_toast_notifier("MyApplicationId")
.
This also described here but in c#.
I just solved this same issue with respect to getting rid of the error but the notification not showing.
This Microsoft doc advises "Important You must include the AppUserModelID of your app’s shortcut on the Start screen each time that you call CreateToastNotifier. If you fail to do this, your toast will not be displayed."
I then followed the instructions I found here to find the AppUserModelID which turned out to be the full path to my Python executable.
For example:
notifier = nManager.create_toast_notifier("C:\...\Programs\Python\Python38\python.exe")
This method is working for me on Python 3.9.1
and pip 21.0.1
Open PowerShell enter following command get-StartApps
it returns the Name
and AppID
-
See the table or image on imgur for reference.
Name AppID
---- -----
Calculator Microsoft.WindowsCalculator_8wekyb3d8bbwe!App
-
This can be handy
get-StartApps | Where-Object {$_.Name -like '*Application Name*'}
get-StartApps | Where-Object {$_.Name -like '*Python*'}
Copy/Paste the AppID
into create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App")
Example:
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
It works after upgrading to 21H2
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>
<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))
I am trying to make winRT send notifications.
I tried doing this to make a notification:
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))
Hoever, it returns this error, when i try to run it.
notifier = notifications.ToastNotificationManager.create_toast_notifier()
RuntimeError: Element not found.
My system meets the requierments of winrt
Windows 10, October 2018 Update or later.
Python for Windows, version 3.7 or later
pip, version 19 or later
How do i fix this error? I can not use other modules, because winrt is the olny one (that I know of) where you can create ui elements on the notification such as buttons.
The exception happens because you need to provide an applicationID
in create_toast_notifier()
Like this for example create_toast_notifier("MyApplicationId")
.
This also described here but in c#.
I just solved this same issue with respect to getting rid of the error but the notification not showing.
This Microsoft doc advises "Important You must include the AppUserModelID of your app’s shortcut on the Start screen each time that you call CreateToastNotifier. If you fail to do this, your toast will not be displayed."
I then followed the instructions I found here to find the AppUserModelID which turned out to be the full path to my Python executable.
For example:
notifier = nManager.create_toast_notifier("C:\...\Programs\Python\Python38\python.exe")
This method is working for me on Python 3.9.1
and pip 21.0.1
Open PowerShell enter following command get-StartApps
it returns the Name
and AppID
-
See the table or image on imgur for reference.
Name AppID ---- ----- Calculator Microsoft.WindowsCalculator_8wekyb3d8bbwe!App
-
This can be handy
get-StartApps | Where-Object {$_.Name -like '*Application Name*'}
get-StartApps | Where-Object {$_.Name -like '*Python*'}
Copy/Paste the AppID
into create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App")
Example:
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
It works after upgrading to 21H2
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>
<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))