System notifications from Mono

Sometime ago, I published a small post about the notify-send command, which lets you show a notification balloon with a custom message using the notification daemon (which uses DBus and is accessed through libnotify), this is handy for notifying the user of an event in a non-intrusive way (he/she doesn’t need to click it to make it disappear). Several Gnome programs such as Epiphany (for showing an incoming message), Evolution (new email notification), etc. use it, but what if you’re willing to use it inside a Mono application you’re writing yourself? One way would be to execute the notify-send command from within your application, but there is a nicer way: libnotify-sharp. This is a library that encapsulates the necessary bindings to libnotify so that it can be used within Mono/C# applications. In Ubuntu 10.04, all we need to be able to use it is to install the libnotify-cil-dev package, then add a reference to notify-sharp:


Note: actually, you only need to install the libnotify0.4-cil package (it is installed automatically when installing libnotify-cil-dev), but then, it won’t appear in the Mono packages list (because Mono uses pkg-config to build this list, and libnotify-cil-dev is the one that provides pkg-config information for notify-sharp), but you can add it “manually” by indicating the path to the notify-sharp dll:

And then write some code:

using Notifications;
 
//...
 
// Instantiate a notification
Notification myMessage = new Notification();
 
// Set title and message
myMessage.Summary = "About";
myMessage.Body = "This is an about message.";
 
// Set icon name
myMessage.IconName = Stock.About; // Comment this if you don't want an icon to appear
 
// Finally, show the notification
myMessage.Show();

These are a couple of screenshots of my test application:

Download the test project here: [download id=”3″]

Leave a Reply