Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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" [] {} 20

this 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_id

and 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.

share|improve this answer

Use update method and call show method again:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import pynotify
import time

pynotify.init("Basic")

n = pynotify.Notification("Title1", "body1", "dialog-warning")
n.show()

time.sleep(1)

n.update("Title2", "body2", "dialog-warning")
n.show()

Update:

There is close method but... it doesn't work at all. Apparently, not only for me:

I thought that it may be caused by the error in bindings so I created this C++ program:

#include <libnotify/notify.h>
#include <unistd.h>

int main(int argc, char **argv) {

    GError *error = NULL;
    notify_init("basic");

    NotifyNotification *example;
    example = notify_notification_new("Ttile1", "Body1", NULL);

    notify_notification_show(example, &error);
    usleep(1000000);

    notify_notification_update(example, "Ttile2", "Body2", NULL);
    notify_notification_show(example, &error);

    usleep(1000000);
    notify_notification_close(example, &error);
}

And compiled it with:

g++ test.cpp -o test $(pkg-config --libs --cflags libnotify)

It didn't work as well. Looks like there is something bugged here.

Next I've tried to use pickle module to save reference - it doesn't work as well. You cannot store reference to those.

So the only thing I can think of is splitting your script into two: daemon and client. After running client should communicate with daemon that has been running all the time in the background with reference to notification instance.

share|improve this answer
    
What if I no longer have a reference to the notification? IE: They run the script once, then again. Can I find an already existing notification? –  Naftuli Tzvi Kay May 10 '13 at 20:47
    
I updated my answer. –  Nykakin May 10 '13 at 22:03
    
Good effort, my friend. So there's no way to update a notification after it's been made and its reference has been lost? –  Naftuli Tzvi Kay May 11 '13 at 0:15
    
Maybe there is but unfortunately I'm not familiar with any method. You can take a look how this library is actually implemented. –  Nykakin May 11 '13 at 1:22
    
actually ".close()" works for me (as long as there is a reference of course) by now (Fedora 20, Gnome 3.10.2, GTK3 3.10.6) so the bug seems to be fixed. However I'd like to point out that everyone should use the new gobject-based notification instead of direct pynotify (wiki.archlinux.org/index.php/Desktop_notifications#Python) –  Felix Schwarz Mar 11 at 23:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.