Is there any way to disable the notification sound on win10toast python library?

Viewed 1450

I am using win10toast to make a notification popup for windows. Is there any way to make the notification silent? In other words, can I disable the sound for the notification I am creating? Can I alternatively change the sound?

Edit: added sample code

My sample code:

from win10toast import ToastNotifier


toaster = ToastNotifier()
for i in range(0,70000000):
    pass
toaster.show_toast("Hey User",
                   "The program is running pretty well. You should try to disable audio on me next though!",
                   icon_path=None,
                   duration=5)
1 Answers

You have to modify the source code of the library in order to do this. Go to the folder where the library was installed and open "__init__.py" file. At the top, after all "win32gui" imports are put, write from win32gui import NIIF_NOSOUND.

After this, go to line 107 and you should see this piece of code:

Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO,
                                  WM_USER + 20,
                                  hicon, "Balloon Tooltip", msg, 200,
                                  title))

After the "title" parameter, put "NIIF_NOSOUND", it should look like this:

Shell_NotifyIcon(NIM_MODIFY, (self.hwnd, 0, NIF_INFO,
                                  WM_USER + 20,
                                  hicon, "Balloon Tooltip", msg, 200,
                                  title, NIIF_NOSOUND))

If you want to do this alternatively you must further modify the source code, you can add a new argument to show_toast method. Something like this:

# line 121
def show_toast(self, title="Notification", msg="Here comes the message",
                icon_path=None, duration=5, threaded=False, sound=False):

And also further send the "sound" parameter:

 # line 130
 if not threaded:
     self._show_toast(title, msg, icon_path, duration, sound)
 else:
     if self.notification_active():
         # We have an active notification, let is finish so we don't spam them
         return False

     self._thread = threading.Thread(target=self._show_toast, args=(title, msg, icon_path, duration, sound))
     self._thread.start()
 return True

Then also add the argument to the "hidden" _show_toast method:

# line 63
def _show_toast(self, title, msg,
                icon_path, duration, sound):

And make an if else statement to check if you should add "NIIF_NOSOUND" flag:

# line 107
Shell_NotifyIcon(NIM_ADD, nid)
data = (self.hwnd, 0, NIF_INFO,
            WM_USER + 20,
            hicon, "Balloon Tooltip", msg, 200,
            title)
if not sound:
    data = data + (NIIF_NOSOUND,)
Shell_NotifyIcon(NIM_MODIFY, data)

This parameter expects a combination of InfoFlags that modify the behavior and appearance of a notification. Read more about NIIF_NOSOUND flag and other flags. Here you can see which "NIIF" flags are available in "pywin32" pywin32 documentation.

You can see more about the parameters of Shell_NotifyIcon function here pywin32 Shell_NotifyIcon.

The second parameter of Shell_NotifyIcon function is a tuple that represents a "PyNOTIFYICONDATA" object which takes different parameters, you can see more about this object here pywin32 PyNOTIFYICONDATA .

Note: This worked for me on Windows 10.

Related