Fun with libnotify
The last few weeks I have been playing a little with libnotify. This is a very handy library that uses the dbus for displaying messages on your desktop.
The easiest way to play around with it is using notify-send. This is a simple command line utility that enables you to send some messages to the dbus:notify-send -u normal -t 10000 -i info “hw” “Hello world”This tells, via the dbus, the notification daemon to display the message with summary “hw” and text “Hello world” for 10 seconds (10.000 milliseconds). It uses the stock icon “info” for it and the urgency is normal. On my system
it looks like this:
What is more interesting is to check out all of the options you can use with notify-send.
The --help (and manual page) talk about to more options --category (-c) and --hint (-h).
In my opinion it is not directly clear what these options expect for parameters and what can be done with these. So let’s find out.
-c, –category=TYPE[,TYPE...] Specifies the notification category.TYPE? Wat TYPE? The manual page refers to The Desktop Notification Spec on http://www.galago-project.org/specs/notification/. Let’s have a look there. In section 6 the category is specified:
Notifications can optionally have a type indicator. Although neither client or nor server must support this, some may choose to. Those servers implementing categories may use them to intelligently display the notification in a certain way, or group notifications of similar types.
So the type can be, for example: device or im. In my case the notify daemon does not seem to support categories.
Time to check out the other option:-h, –hint=TYPE:NAME:VALUE Specifies basic extra data to pass. Valid types are int, double, string and byte.On The Desktop Notification Spec the hints are also described. If you want to display
the message on the upper left corner of your screen, you could use something like:
notify-send -h int:x:10 -h int:y:10 “hw” “Hello world” The other options described in the spec also seem to be interesting is sound-file. Unfortunately my dbus server does not seem to support this type of hint. I would expect that a hint like string:sound-file:/usr/share/sounds/question.wav would try to play the sound file /usr/share/sounds/question.wav. The program dbus-monitor, which is very handy for debugging the dbus, does show that the hint reaches the dbus server:
dict entry( string “sound-file”In the previous hint it showed:
variant string “/usr/share/sounds/question.wav”
)
dict entry( string “x”I suppose either my syntax is wrong or the dbus server does not support the sound-file.
variant int32 10
)
dict entry(
string “y”
variant int32 10
)
Nevertheless I am using notify-send (part of libnotify-bin in Debian based systems) and the perl variant Gtk2::Notify very often.
Comments
Comment from Josh Patton
Time: December 9, 2008, 2:24 am
Thank you for this post!
I was trying to find out how to pop up a notification, googling “libnotify command line”
I am using this for the gnome-sensors applet, to pop a warning when my laptop’s temperature goes too high.
Comment from Ahmad Syukri
Time: February 17, 2009, 10:10 pm
As for sound support, see if your notification-daemon is built with –enable-sound or not. Mine didn’t, so I had to rebuild. You’ll need gstreamer headers for that.
Comment from mescalinum
Time: June 19, 2009, 1:28 pm
this is very nice!!
I’m using it for monitoring my syslog for certain keywords:
tail -n0 -f /var/log/messages | grep -i -e failed -e error -e login | while read line; do notify-send -u normal -t 10000 -i error “syslog” “$line”; done
Comment from eric
Time: July 27, 2009, 3:01 pm
thank you for this nice tip.
I would just add that if you set -t 0 this will show an “infinite” notification.
I’m using it in my backup script running under crontab every day at work. just telling me that the backup has started, and that the same backup has finished !
Comment from eric
Time: July 27, 2009, 3:03 pm
addendum : you can use simple html formatting (like bold, italic)

Comment from B10m
Time: September 12, 2008, 10:34 pm
The whole notify-send is fairly interesting! I currently use it for applications running in my VNC session. Since I don’t always look at that screen, this seems to work nice:
DISPLAY=:0 notify-send -u normal -t 10000 -i info "header" "body"Now maybe, just maybe, this would be fun as well for my mailserver. A biff’ish script informing me about new mail (?).