Notification icon in Ubuntu with Unity and Mono C# example

Ubuntu Unity changed the way notification icons are handled and StatusIcon object no longer works for Mono and C#. In order to make it work you have to use ApplicationIndicator object with newer version of Ubuntu. It seems easy enough when you know how to do it, but I couldn’t find a full example anywhere on the internet. Here is one now.

First you need to install the following packages:

apt-get install libappindicator0.1-cil-dev libappindicator0.1-cil

Add reference to your project for appindicator-sharp and include this at the top of the souce file where you are going to be using the indicator:

using AppIndicator;

Build the indicator and the menu based on the sample like this:

private String _ExecutableFolder = "";
private String ExecutableFolder {
	get {
		if (_ExecutableFolder == "")
			_ExecutableFolder = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
			return _ExecutableFolder;
	}
}

private void BuildMenu()
{
	ApplicationIndicator indicator =
		new ApplicationIndicator (
		"sample-application",         //id of the the indicator icon
		"app-icon",                    //file name of the icon (will look for app-icon.png)
		Category.ApplicationStatus,
		ExecutableFolder            //the folder where to look for app-icon.png
	);

	//Build Popup Menu for ApplicationIndicator
	Menu popupMenu = new Menu ();

	//Show menu item
	ImageMenuItem menuItemShow = new ImageMenuItem ("Show");
	menuItemShow.Image = new Gtk.Image(Stock.Info, IconSize.Menu);
	menuItemShow.Activated += (sender, e) => this.Visible = !this.Visible;
	popupMenu.Append(menuItemShow);

	popupMenu.Append(new SeparatorMenuItem());

	//Quit menu item
	ImageMenuItem menuItemQuit = new ImageMenuItem ("Quit");
	menuItemQuit.Image = new Gtk.Image (Stock.Quit, IconSize.Menu);
	menuItemQuit.Activated += (sender, e) => Application.Quit ();
	popupMenu.Append (menuItemQuit);

	popupMenu.ShowAll();

	//Assign menu and make indicator active
	indicator.Menu = popupMenu;
	indicator.Status = AppIndicator.Status.Active;
}

Add app-icon.png to your project and make sure it’s copied over the build folder. Include BuildMenu() function somewhere in the constructor of you main window and run the application.

Full sample download is available here: NotificationIcon.zip. You will need MonoDevelop to open and build the project. Tested and running on Ubuntu 12.04.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed.

5 thoughts on “Notification icon in Ubuntu with Unity and Mono C# example”

  1. Hi,
    thanks for this tutorial.
    I want to hide my application window and display only the indicator how can I do that ?

  2. I just wanted to let you know that I love you for this example. Documentation is scarce and where it exists, it’s vague or confusing.

Leave a Reply to Nasreddine Cancel reply

Your email address will not be published. Required fields are marked *