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!
Look inside the “tests” directory in the libnotify source code tarball.
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
That’s right, it could have been done more simply, just have a look at the libnotify source code !
I know that is a good way, but not the proper way of developing something. From a developer’s angle, a toolkit/sdk is as useful as it’s documentation quality.
People usually don’t have much time to dig deep into the source code.
You need add gtk_main() to keep the icon visible.
Pingback: Neck deep into technical affairs < Manish’s Scribble Board
check out zenity project page, there’s an example code for displaying notification http://live.gnome.org/Zenity.
The page does not exist. The mistake you made was putting a period after the URI.
It should be http://live.gnome.org/Zenity
Pingback: Jaunty Notifications, but why did Ted Dzuiba bash it? « Manish’s Tech Blog
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
ie
dash dash cflags
dash dash libs
(doh it messed the comment too)
It is a problem with wordpress IMO. It should be probably this
I also searched for some documentation on libnotify and couldn’t find much.
Thank you very much for a working example
oh, and that “dash dash” problem kept me wondering what’s wrong for about 5 minutes
Sweet! My pleasure
Pingback: [C/C++] Show notification "balloon" on tray icon -- portable way?
I’m also interested in this topic but I have spent hours and hours googling only to find a feeling of frustration. This is the first web page I found useful for this topic. Big thanks to you!
by the way the code doesn’t fully work on my ubuntu, I guess the libnotify has changed some since. Here is the working verison modified from your code:
void showNotification()
{
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);
// 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 use code blocks 10.05 and gcc version 4.5.2
Howard
Thanks for your very useful page : as Howard said, not so much about this library on the net
Please note that some functions seems dis-appeared after you created your code. Especially (and unfortunately
), notify_notification_attach_to_status_icon()
Bye