Working with libnotify

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!

About these ads

17 thoughts on “Working with libnotify

    1. 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 :)

        1. 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.

  1. 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

    1. It is a problem with wordpress IMO. It should be probably this

      $ gcc `pkg-config --cflags --libs gtk+-2.0` notify.c -o notify -l notify
  2. 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 :D

  3. 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

  4. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s