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.
int main(int argc, char **argv)
// initialize gtk
char name = “Sample Notification”;
// initiate notify
// create a new notification
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);
// attach that icon to the notification
// set the timeout of the notification to 3 secs
// set the category so as to tell what kind it is
char category = “Testing Notifications”;
// set the urgency level of the notification
GError *error = NULL;
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,
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!