6

I've been using win32api in Python3 to create a Windows 10 application that supports toast notifications.

I already have a system tray icon for my app, I'm adding toast notifications using the following code

def show_toast(self,msg,title):
        flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
        nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, self.hicon, 
            "SpotiFind")
        win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, (self.hwnd, 0, 
            win32gui.NIF_INFO,
            win32con.WM_USER + 20,
            self.hicon, "Balloon Tooltip", msg, 200, title))        

All works well but the notification stays in the notification area and I want to delete it automatically... according to MSDN (https://learn.microsoft.com/en-us/windows/desktop/api/shellapi/ns-shellapi-_notifyicondataa)

To remove a balloon notification, specify NIF_INFO and provide an empty string through szInfo.

So I've tried the following

def _destroy_toast(self):
    win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, (self.hwnd, 0, 
            win32gui.NIF_INFO,
            win32con.WM_USER + 20,
            self.hicon, "Balloon Tooltip", "", 200, ""))

This does nothing...

Thanks in advance..

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
TOC666
  • 101
  • 4
  • 1
    Use `Shell_NotifyIcon(NIM_DELETE, nid)` The tabs are in the wrong place in your post. – Barmak Shemirani Nov 16 '18 at 10:43
  • Tabs are due to SO requirements.. but there's not NIM_DELETE in the snippet... NIM_DELETE is used to delete the ICON and I want to keep the icon (system tray) and just remove the toast notification from the notification area.. – TOC666 Nov 16 '18 at 10:52
  • I can't duplicate the described behavior with `NIM_MODIFY` , it removes the toast in my Win10 – Barmak Shemirani Nov 16 '18 at 20:05
  • The toast disappears by itself. but it stays in the notification area. [link](https://imgur.com/a/2bs4bVQ) I want it to be removed from the windows 10 notification area when the toast is done... – TOC666 Nov 16 '18 at 22:59

1 Answers1

0

I tried to implement this in C# and also had problems with it. The way that works for me is:

Shell_NotifyIcon(NIM_DELETE, nid)
Shell_NotifyIcon(NIM_CREATE, nid)

During deletion, all notifications will be cleared, both active and snoozed.

Konstantin S.
  • 1,307
  • 14
  • 19