Skip navigation.
Home

Desktop Notifications (using libnotify)

Desktop Notifications is a system for consolidating an API, UI and mechanism to allow applications, applets, services, etc. to notify the user when something interesting happens.

The software for this is currently hosted under the Galago project, which is listed on freedesktop.org.

This software consists of a notification-daemon, a client API implemented by the libnotify library, the DBus system for tying things together, and finally various language bindings (so far only python and of course C)

The simplest way to add notifications to your app is to call the notify-send utility. But this is quite limited, and so not very interesting.

To use libnotify in a python application do the following:

try:
    import pynotify
    if pynotify.init("My Application Name"):
        n = pynotify.Notification("Title", "message")
        n.show()
    else:
        print "there was a problem initializing the pynotify module"
except:
    print "you don't seem to have pynotify installed"

You can set the urgency level to one of three values using the following:

        n.set_urgency(pynotify.URGENCY_LOW)
        n.set_urgency(pynotify.URGENCY_NORMAL)
        n.set_urgency(pynotify.URGENCY_CRITICAL)

One option to the Notification() call is to add an icon. This can use one of three methods:
1. a URI specifying the icon file name (e.g. file://path/to/my-icon.png)
2. a 'stock' icon name. One that would succeed in a call to gtk_icontheme_lookup() (e.g. 'stock-delete') Note: these are not necessarily normal GTK stock icons - any theme icon will work.
3. a pixbuf

For the first two methods, just specify the icon name or URI as the 3rd parameter to the Notification() call.

        n = pynotify.Notification("Title", "message", "icon-name")

For the pixbuf method use: (where 'icon' is a pixbuf)

        n.set_icon_from_pixbuf(icon)

To set the timeout value for the displayed message:

        n.set_timeout(seconds)

To position the message (e.g. to associate it with a toolbar applet)

        n.attach_to_widget(widget) # 'self' often works
      # or position it explicitly
      #  n.set_hint("x", x-coordinate)
      #  n.set_hint("y", y-coordinate)

You can close the notification before it has timed out using

        n.close()

There's lots more you can do, such as putting buttons on the messages and getting callbacks when they are clicked, but that is beyond this tutorial. See the examples that come with the pynotify package to learn more.

Syndicate content