I have written a script which generates notifications like this:notify-send -i audio-card Transferring audio playback to speakers. \    Audio playback has been transferred to the analog output speaker system.Is there any way to clear or to replace these notifications from the command line or from Python? Essentially, I have a script which toggles between two PulseAudio sinks and I'd like to clear previous notifications of this type to replace them with the updated text if the user toggles quickly.
Clearing/modifying previous libnotify notifications?
python;notifications;libnotify
From CLI, you can display and close a notification pop-up via gdbus/qdbus.Here's how to that with gdbus:gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.Notify my_app_name 42 audio-card Message Body [] {} 20this will output something like:(uint32 72,)72 being the notification ID. Now that you know the ID you can close the pop-up window with:gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.CloseNotification 72 Now, if you need the ID later on, just write it to a file when you call Notify:gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.Notify my_app_name 42 audio-card Message Body [] {} 20  | sed 's/[^ ]* //; s/,.//' > /tmp/last_idand get it from there when you want to close the pop-up:gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.CloseNotification $(cat /tmp/last_id)P.S. I'm on Gnome 3 and notifications sent via notify-send, pynotify, libnotify etc only last 5 seconds regardless of the time option (that's the default behaviour in Gnome 3, don't ask me why). Also, they don't stack up: Gnome displays only one notification at a time. So I can't test with several pop-up windows but it should work.