pythongnomepynotify

How to remove an obsolete pynotify notification?


I just started with python and wrote myself a nice, small script that uses gnome-notifications via pynotify, like this:

import pynotify

pynotify.init("Application") 
alert = pynotify.Notification("Title", "Description") 
alert.show();

This works great, but the thing is, when I execute the script twice in a row it takes a while for the first notification to go away. The second gets shown after that. Since the first one is obsolete when i execute the script for the second time, I want to remove the first one programmatically prior to showing the second (or replace it). Is this possible, and if it is, how?

A bit of context to understand why I need this: Since I often switch my mouse from left- to right-handed and the other way around, I want a script that just inverts this preference and tells me in a notification "switched to left-handed" and "switched to right-handed".


Solution

  • I searched around for a while and came to the conclusion that it is not possible in this case.

    You are able to use Notification.update() to update an existing notification object. But you can't query existing ones from the system to modify or hide them. It may be possible to store the object somewhere via serialization and restore it to update. But even then you still have to know the exact duration of the notification and the timestamp when you launched it, since there is no way to test if a notification is still visible.

    A short sample how to use update(). Just for reference, since the pynotify doc seems almost non-existent to me:

    #!/usr/bin/env python
    
    import pynotify
    
    pynotify.init("MyApplication")
    
    a = pynotify.Notification("Test notification", "Lorem ipsum op")
    a.show()
    raw_input("Press return to update the notification")
    a.update("Updated notification", "Ipsum lorem still op")
    a.show()
    

    You have to call show() after the update. Otherwise the changes won't get displayed.

    There is also a close() function in the Notification object, but that doesn't do anything for me (on Linux/Gnome, may be system dependend).