8

I am setting a notification with notify-send in a script. The only problem is that when the script is called several times, all the notifications are added to a notification stack and only called one after the other.

Is there a way to clear all notifications on the screen and display the new one ?

4 Answers 4

5

Unfortunately, you can not clear or "dismiss" the notify-osd notifications. You might have better luck using Zenity instead; it has more options than notify-send.

You could use the --timeout option to dismiss a notification after some number of seconds has passed.

zenity --info --timeout=5 --title="Test Notification" --text "$(date +%Y%m%d-%H%M%S): My notification"

You could also keep a list of process IDs (in an environment variable or file) of previous notifications and send them a HUP signal to clear them out before displaying a new notification.

i=0
pids=
for x in $(seq 1 5); do
    i=$((i + 1))
    zenity --info --title="Test Multiple Notifications" --text "$(date +%Y%m%d-%H%M%S): Notification number $i" &
    pids+="$! "
done
sleep 5
for p in $pids; do kill -HUP $p >/dev/null 2>&1; done
i=$((i + 1))
zenity --info --timeout=2 --title="Test Multiple Notifications" --text "$(date +%Y%m%d-%H%M%S): Notification number $i" &

Or kill all zenity processes before displaying a new notification:

killall zenity
zenity --info --title="Test Notifications" --text "$(date +%Y%m%d-%H%M%S): My notification" &

Or kill certain zenity processes before displaying a new notification:

ps ho pid,args | grep -i 'zenity.\+--title=test notifications' | sed -e 's/^ *\([0-9]\+\).*$/\1/'
zenity --info --title="Test Notifications" --text "$(date +%Y%m%d-%H%M%S): My notification" &
4
  • It would be unfair if I downvoted this, but you are clearly wrong. With regard to notifications, zenity has a far poorer set of options than notify-send. And it does in no way solve the problem, because it also does not give control over the notifications already sent.
    – jankes
    Commented Nov 8, 2011 at 12:11
  • @jankes: Sorry, I originally answered on my mobile and couldn't really elaborate my answer. Edited my answer to elaborate.
    – Go Dan
    Commented Nov 8, 2011 at 13:48
  • What you presented are information dialogs, not notifications. they can be used as a form of substitute, but are of an entirely different nature. To create notifications with zenity, you need to use the --notification option together with --listen.
    – jankes
    Commented Nov 8, 2011 at 14:10
  • @DanCruz It's working, but I get this error: "** (zenity:8624): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged."
    – Dr.jacky
    Commented Mar 11, 2017 at 11:47
18

This is actually feasible using notify-send:

notify-send --hint int:transient:1 "Title" "Body"

By setting the transient hint, the when the notification expires or dismissed, it does not linger around in the notification bar.

1
  • 1
    What exactly does this do differently than just invoking the same command without the --hint int:transient:1 part of the command?
    – zrajm
    Commented May 12, 2020 at 14:07
4

Maybe not the best option but here's an alternative: you can simply kill the notify-osd process and restart it. Then publish your notification.

pkill notify-osd
/usr/lib/x86_64-linux-gnu/notify-osd &
notify-send "Hello!"
1
  • For me, using xfce, the process was not named notify-osd but instead xfce4-notifyd, more specifically /usr/lib/x86_64-linux-gnu/xfce4/notifyd/xfce4-notifyd. I could instead do pkill xfce4-notifyd and it would kill the existing notification. The next time notify-send ran, it auto created the xfce4-notifyd process again, so it seemed safe.
    – Scott
    Commented Aug 15, 2016 at 18:39
2

Unfortunately this does not seem feasible with notifications set by notify-send. Have a look at the source code of notify-send.c - it creates a notification, sets its parameters but does not store any reference to it, anywhere. Instead, it calls g_object_unref, which, as I understand from here, effectively removes the possibility of external interaction with the message. So, to have advanced management power over the notifications, you'd need to use different tool than notify-send (probably a custom application).


A possible crude hack to achieve your goal would be a script using xautomation tools. You could use them to locate the "close" buttons on all the notification pop-ups and simulate a mouse click on each of them. But that's not so easy and certainly not a neat solution.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .