January 5th, 2009
Showing Gnome/Ubuntu LibNotify Notifications from Mono and C# - 1

In an earlier post I looked at how I could add my own icon to the Gnome Notification area from my Mono C# applications. The next thing I wanted to do was to show a notification in the taskbar to the user when something truly important happens. If you are running Ubuntu Intrepid (8.10) then its easy to create them from Mono. As I found out, Hoary (8.04) doesn’t include the required libraries.
The standard Mono distribution does not include support for showing Notifications as this functionality is not part of the Gnome standard distribution. Instead Ubuntu and others depend on DBUS and libnotify to deliver their messages. Sebastian Dröge has created the necessary bindings for Mono/C# in libnotify-sharp and these are contained in the Debian/Ubuntu library libnotify0.4-cil.
To install them run:
sudo apt-get install libnotify0.4-cil
There is also a documentation package called monodoc-notify-sharp-manual which you could install it is however completely empty.
To run the demo code (below) simply create a new empty GTK solution in MonoDevelop. You will need to include a reference to libnotify-sharp. In the “Solution” pane, right click “References” and add the “notify-sharp” binding as shown below.
Creating a simple notification is straightforward:
using Notifications;
....
Notification myNote = new Notification();
// Use a stock icon, and set the title (summary) and message (body)
myNote.IconName = Stock.Harddisk;
myNote.Summary = "Harddisk fatal error";
myNote.Body = "Internal moist levels critical, please man the pumps!";
Each notification has a title (called the summary) and a body. We can also optionally specify an icon and in the code above I have used a stock Gnome icon.
The “AddAction” method allows you to specify one or more actions for a user to click on when the notification is displayed. As the notifications are only displayed for a very short period of time this is probably not such a good place if the required action is truly important.
// Add an action the user can take to remedy the situation
myNote.AddAction("DoPump","Start Pump",OnDoSomething);
myNote.AddAction("DoRun","Run!",OnDoSomething);
The first parameter is passed to the user specified handling function. The second is the name of the action as displayed on the notification.
There are three levels of urgency for our notifications: Low, Normal and Critical. The difference to your user is probably just in the color of the notification.
// The color of the notification changes depending on urgency
myNote.Urgency = Urgency.Critical;
Finally we need to show the notification:
// Show the notification on the desktop
myNote.Show();
Wrapping up
Unrelated to displaying notification messages from Mono I came across a post by Luc Castera showing how you can simply show these notifications from a shell script:
- Install libnotify-bin: sudo aptitude install libnotify-bin
- From a script, call it: notify-send ‘title of notification’ ‘body of notification’
Source Code
If you created an empty GTK project from MonoDevelop then the following should replace MainWindow.CS:
// MainWindow.cs created with MonoDevelop
// User: martijn at 10:04 AM 1/5/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using Gtk;
using Notifications;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
Button myButton = new Button("Send Notification");
myButton.Clicked += OnButtonClickedEvent;
myButton.Show();
Add(myButton);
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected void OnButtonClickedEvent (object sender, EventArgs a)
{
Notification myNote = new Notification();
// Use a stock icon, and set the title (summary) and message (body)
myNote.IconName = Stock.Harddisk;
myNote.Summary = "Harddisk fatal error";
myNote.Body = "Internal moist levels critical, please man the pumps!";
// Add an action the user can take to remedy the situation
myNote.AddAction("DoPump","Start Pump",OnDoSomething);
myNote.AddAction("DoRun","Run!",OnDoSomething);
// The color of the notification changes depending on urgency
myNote.Urgency = Urgency.Critical;
// Show the notification on the desktop
myNote.Show();
}
protected void OnDoSomething( object sender, ActionArgs args )
{
string TheResult = "";
switch((string)args.Action)
{
case "DoPump" : TheResult = "Pumping failed, head for the hills!"; break;
case "DoRun" : TheResult = "Hiding is fine, come back later!"; break;
default: TheResult = "Not supposed to happen!"; break;
}
MessageDialog myDialog = new MessageDialog(this,
DialogFlags.DestroyWithParent,
MessageType.Error,
ButtonsType.Ok,
TheResult );
myDialog.Run();
myDialog.Destroy();
}
}










Except where otherwise noted, content on this site is
July 15th, 2009 at 2:56 am
Hey, I tried your example but I noticed when trying to place buttons on the notification area it would display as a regular dialog and not the sleek notification bubble. If I removed the Actions on the bubble it would display normally. You know why this is happening?