the force that pushes object up
Random header image... Refresh for more!

Using libnotify in Ubuntu 9.04

There are some applications which need to send notifications to users. Ubuntu 9.04 has new on-screen-display notification agent called Notify OSD which implmeents freedesktop.org Desktop Notifications Specification with semi-transparent click-through bubbles. This post is not about Notify OSD, but about how you can use libnotify to send notifications in you shell scripts, C programs and Python programs.

Send notifications from your shell scripts

You have to install libnotify-bin package in Ubuntu and you can use notify-send command to send notifications from your shell script. After installing libnotify-bin you can try notify-send command just like following.

notify-send "First Notification"

By default the message will be displayed for 5 seconds. To change how long a message stays displayed use the “-t” switch. You can use “-t 0″ leave the message up until the user closes it. But in new Ubuntu notification agent will display these notifications as alert box. You can find great explanations about Notify OSD here.

notify-send "This message will be displayed for 3 seconds" -t 3000
notify-send "Click me to close me." -t 0

Add title to your notification like following:

notify-send "This is the Title"  "This is the message body"

Add icon with title and message body(You can find icon codes here):

notify-send "New Mail" "Hey buddy, you got a new E-mail" -i notification-message-email

Send notifications from your Python application

You need to install python-notify package first and then you can use pynotify to send notification from your Python script. There are different capabilities in different notification agents. I am ignoring getting those information in this example.

#!/usr/bin/python

import sys
import pynotify

if __name__ == "__main__":
    # Check whether your notification agent support
    # incon-summary-body layout.
    if not pynotify.init("icon-summary-body"):
        sys.exit(1)

    n = pynotify.Notification(
            "You have a mail",
            "You have new e-mail from Milinda",
            "notification-message-email")
    n.show()

Sample code in C
You need to install libnotify-dev, libglib2.0-dev and libgtk2.0-dev packages first. You can compile this sample using following gcc command.

gcc test_notify.c `pkg-config --cflags glib-2.0` `pkg-config --cflags --libs gtk+-2.0` -lnotify
#include
#include
#include

void closed_handler (NotifyNotification* notification, gpointer data){
    g_print ("closed_handler() called");
    return;
}

int main(int argc, char** argv){
    NotifyNotification* notification;
    gboolean            success;
    GError*             error = NULL;

    if (!notify_init ("icon-summary-body")){
        return 1;
    }

    notification = notify_notification_new (
                 "You have a mail",
                 "You have mail from Milinda",
                 "notification-message-email",
                 NULL);

    error = NULL;
    success = notify_notification_show (notification, &error);
    if (!success)
        g_print ("That did not work ... \"%s\".\n", error->message);

    g_signal_connect (G_OBJECT (notification),
        "closed",
        G_CALLBACK (closed_handler),
        NULL);
    notify_uninit ();

    return 0;
}

For more information please refer Ubuntu Notification Development Guidelines document.

September 12, 2009   4 Comments