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.
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 "Click me to close me." -t 0
Add title to your notification like following:
Add icon with title and message body(You can find icon codes here):
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.
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.
#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