March 29, 2009...7:50 PM

Working with libnotify

Jump to Comments

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


Leave a Reply