Quantum Sushi
Quantum Sushi

Reputation: 514

Windows notifications in python

I'm trying to make a toast notification on win 10 with python. However, every time I try, I got an error that involves something called "shell notify icon". And when I mean every time... I tried with every library I could find ! Notifypy, pynotifier, win10toast, plyer, etc... Every time, the same or approximatively same error involving shell notify icon...

Here is one of those errors :

>>> Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\Elève\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\Elève\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Elève\AppData\Local\Programs\Python\Python37-32\lib\site-packages\plyer\platforms\win\libs\balloontip.py", line 206, in balloon_tip
    WindowsBalloonTip(**kwargs)
  File "C:\Users\Elève\AppData\Local\Programs\Python\Python37-32\lib\site-packages\plyer\platforms\win\libs\balloontip.py", line 139, in __init__
    self.notify(title, message, app_name)
  File "C:\Users\Elève\AppData\Local\Programs\Python\Python37-32\lib\site-packages\plyer\platforms\win\libs\balloontip.py", line 188, in notify
    raise Exception('Shell_NotifyIconW failed.')
Exception: Shell_NotifyIconW failed.

Now look at the first line, it says something about Threads ! That's because I played with threads for my application, do you think that's why it's messed up ? How could I see and clean all my threads ?

Some people already asked this question, here :

pywintypes.error: (-2147467259, 'Shell_NotifyIcon', 'Unspecified error') win10toast python

But nobody answered, and here is another one where the creator of the library answered but didn't help :

Why is this simple python toast notification not working?

On this last link, the solution says :

I discovered the solution to my question.
The toast notifications have been suppressed by Windows 10 Action Center.
At the bottom right corner, click the Action Center icon.
For my PC, the "Quiet Hours" setting was turned on. After I disable "Quiet Hours",
toast notifications can appear.

The python library win10toast works perfectly fine after Action Center settings are correctly set

But my code was working a bit before all those errors started appearing, and I could show some toasts, so that's not the point... Here's my code by the way :

from pynotifier import Notification
Notification(title='HEAD', description="BODY", duration=3, urgency=Notification.URGENCY_CRITICAL).send()

It was working fine, as I said... Until I started touching to the icon_path argument, because I wanted to add an icon. Since then, errors... Is "shell notify icon" related to the icon I tried to change ? I don't even know really what it is !

So, if you could help me get a stable and working code, that would be awesome ! Thanks by advance.

EDIT : Ok now real wtf... I restarted my computer, it worked perfectly, and later on the day, stoped working... The worked again... And so on ! @.@

Upvotes: 2

Views: 1302

Answers (2)

N3RDIUM
N3RDIUM

Reputation: 366

You can also use winrt like this:

import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
import sys

#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(sys.executable)

#define your notification as

tString = """
<toast>

    <visual>
        <binding template='ToastGeneric'>
            <text>New notifications</text>
            <text>Text</text>
            <text>Second text</text>
        </binding>
    </visual>

    <actions>
        <input id="textBox" type="text" placeHolderContent="Type a reply"/>
        <action
            content="Send"
            arguments="action=reply&amp;convId=01"
            activationType="background"
            hint-inputId="textBox"/>
            
        <action
            content="Button 1"
            arguments="action=viewdetails&amp;contentId=02"
            activationType="foreground"/>
    </actions>

</toast>
"""

#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)
notification = notifications.ToastNotification(xDoc)

# this is not called on the main thread.
def handle_activated(sender, _):
    print('Button was pressed!')

# add the activation token.
activated_token = notification.add_activated(handle_activated)

#display notification
notifier.show(notification)

while True:
    pass


Also, please note that handle_activated will not be called on the main thread, and this does not work in IDLE.

Upvotes: 1

Quantum Sushi
Quantum Sushi

Reputation: 514

Well, seems everything fixed by itself... I now use :

from plyer import notification 
notification.notify('Localisation :', 'France')

But I really don't know where my errors were coming from... Well, all those notifications libraries seemed pretty unstable, telling me "unspecified error" and so... Plyer seems way more stable.

Upvotes: 1

Related Questions