How to raise user notifications in Ubuntu using java code?

Solution 1:

You can use java-gnome, a java binding for GTK and GNOME to display notify-osd notifications. You'll need to install the library first:

sudo apt-get install libjava-gnome-java libjava-gnome-java-doc   

Here is a quick example:

import org.gnome.gtk.Gtk;
import org.gnome.notify.Notification;
import org.gnome.notify.Notify;

public class notifyTest {
    public static void main(String[] args) {  

        Gtk.init(args); // initialize Gtk
        Notify.init("Program Name"); // initalize the notification system  

        Notification myNotification = new Notification("Hello world!", "This is an example notification.", "dialog-information"); // create the notification object
        myNotification.show(); // show the notification  

    }


}

The general format for a notification is this:

Notification someName = new Notification("Summary", "Body", "Icon")  

Both the body and icon fields can be null, but there must be a summary. For a list of icons you can use by default check the Notify-OSD page of the Ubuntu Wiki.

You then call:

someName.show();  

To display the notification. For more information see the java-gnome Notify and Notification doc pages.

Note: You must have both Gtk and Notify initialized before you can send any notifications.