Usually when using GNOME, you must have come across notification boxes which show near the time area of the upper panel. Ever wondered how it works? It is handled by the library named as libnotify. It has many bindings to be used with various languages, two most prominent ones being C and Python.

The python binding is very very simple, the real problem is using it with C. I searched the whole internet in hope of finding a good tutorial or code example to give us at last. The only help i could get was the documentation at GNOME. After giving it random attempts, finally I managed to hit the eye
Here is the code which I have committed to my personal svn dump at Google Code. Have a look at the code, the comments are enough to give you a basic idea how it works. I still recommend you to download the code from link I gave just now instead the one shown below.
#include<libnotify/notify.h>
int main(int argc, char **argv)
{
// initialize gtk
gtk_init(&argc,&argv);char name[40] = “Sample Notification”;
// initiate notify
notify_init(name);// create a new notification
NotifyNotification *example;
example = notify_notification_new(name,“Checking it out”,NULL,NULL);/* Status Icon is not working properly */
// create an icon for the notification
GtkStatusIcon *icon = gtk_status_icon_new_from_stock (GTK_STOCK_YES);
gtk_status_icon_set_visible(icon,TRUE);
// attach that icon to the notification
notify_notification_attach_to_status_icon (example,icon);// set the timeout of the notification to 3 secs
notify_notification_set_timeout(example,3000);// set the category so as to tell what kind it is
char category[30] = “Testing Notifications”;
notify_notification_set_category(example,category);// set the urgency level of the notification
notify_notification_set_urgency (example,NOTIFY_URGENCY_CRITICAL);GError *error = NULL;
notify_notification_show(example,&error);
}
I am still not able to make the icons show up properly. I need to work again on it, reading the documentation once more. Frankly, GNOME needs to improve its documentation. Just bringing forward the fact that they are volunteers, doesn’t suffice.
Yeah, for compiling the above, you need to have the GTK+ development headers installed on your system. It’s called as libgtk2.0-dev on Ubuntu, somewhat similar for other distos. Use this command to compile your program assuming that your program is saved in a file named notify.c
$ gcc `pkg-config –cflags –libs gtk+-2.0` notify.c -o notify -l notify
Make sure that the quote encircling pkg-config is back-ticks not simple quotes. Now run the program,
$ ./notify
and you can see a notification box coming on the right top corner.
Another news is that Ubuntu Jaunty Jackalope 9.04 is going to have new notification system, which is obviously less obstructive and looks better with transparency.
![]()
Finally thanks to #pygtk on irc.freenode.net
IRC FTW!




10 Comments
March 29, 2009 at 8:16 PM
Look inside the “tests” directory in the libnotify source code tarball.
March 29, 2009 at 8:21 PM
Sayamindu,
Thanks for pointing it out. Looks like I need to download the source code tarball for each library I want to use.
There is hardly any tutorial on the internet except its python bindings.
It’s very very simple to use via Python, 3 lines of code for a basic notification
April 2, 2009 at 7:10 PM
You need add gtk_main() to keep the icon visible.
April 4, 2009 at 3:34 AM
[...] have written an article on Working with libnotify. libnotify is a library for Gnome desktop environment to show notifications to the [...]
April 10, 2009 at 12:04 AM
check out zenity project page, there’s an example code for displaying notification http://live.gnome.org/Zenity.
April 10, 2009 at 11:43 AM
The page does not exist. The mistake you made was putting a period after the URI.
It should be http://live.gnome.org/Zenity
May 9, 2009 at 4:08 PM
[...] on that retarded close button everytime I wanted that shit to go away from my screen. I have even written a post how to use the old libnotify library, still I was never much amazed at it. When I came to know about some new notification system in [...]
May 27, 2009 at 12:05 PM
You can’t copy + paste the code from this pag because the html messes up some chars such as speechmarks, goto the google code page and that will work.
Also the html messes up the gcc line to compile the -cflags -libs part should be TWO DASHES –cflags –libs
May 27, 2009 at 12:06 PM
ie
dash dash cflags
dash dash libs
(doh it messed the comment too)
May 27, 2009 at 7:50 PM
It is a problem with wordpress IMO. It should be probably this