Difference between revisions of "Desktop notifications"

From ArchWiki
Jump to: navigation, search
(Apply style guides)
(add article summary, java example, reorganize notification servers)
Line 1: Line 1:
 
[[Category:Development (English)]]
 
[[Category:Development (English)]]
 
{{i18n|Libnotify}}
 
{{i18n|Libnotify}}
 +
 +
{{Article summary start|Summary}}
 +
{{Article summary text|This article discusses how to install and configure libnotify. Libnotify is a small and lightweight library that implements the Desktop Notifications Specification. This article covers how to get libnotify on your system and how to use it for application development.}}
 +
{{Article summary heading|Related}}
 +
{{Article summary wiki|GTK+}}
 +
{{Article summary wiki|Libcanberra}}
 +
{{Article summary end}}
 +
 
'''Libnotify''' is an easy way to display desktop notifications and information in a small dialog. It's used in much open source apps like Evolution, Pidgin, etc. It has support for Gtk+ and Qt applications and is desktop independent.
 
'''Libnotify''' is an easy way to display desktop notifications and information in a small dialog. It's used in much open source apps like Evolution, Pidgin, etc. It has support for Gtk+ and Qt applications and is desktop independent.
  
Line 9: Line 17:
 
In order to use libnotify, you have to install a notification server:
 
In order to use libnotify, you have to install a notification server:
  
===GNOME===
+
===GNOME and KDE===
 +
[[GNOME]] and [[KDE]] use their own implementations to display notifications, and you can't replace them. Their notification servers are started automatically on login to receive notifications from applications via DBus.
 
*{{Pkg|gnome-shell}} provides a notification server itself. Notifications are displayed at the bottom of the screen.
 
*{{Pkg|gnome-shell}} provides a notification server itself. Notifications are displayed at the bottom of the screen.
*In GNOME Fallback mode, you need to [[pacman|install]] {{Pkg|notification-daemon}}, available in the [[Official Repositories]].
+
*In GNOME Fallback mode, {{pkg|notification-daemon}} provides the notification server. Notifications are displayed at the top right corner of the screen.
:{{Note|notification-daemon is a required component for GNOME Fallback mode. You can't login without it.}}
+
*KDE uses knotify4 from package {{Pkg|kdebase-runtime}} to display notifications. Notifications are displayed at the bottom right corner of the screen.
 
+
===KDE===
+
KDE uses knotify4 from {{Pkg|kdebase-runtime}} package to display notifications. If you are using KDE, this package is already installed.
+
 
+
===XFCE===
+
To get notifications in XFCE, you need to [[pacman|install]] {{Pkg|xfce4-notifyd}}, available in the [[Official Repositories]].
+
{{Tip|To configure xfce4-notifyd, run the following command in the terminal: {{ic|xfce4-notifyd-config}}.}}
+
 
+
===Unity===
+
Unity uses [[Notify OSD]], which is available in [[Arch User Repository]]: {{AUR|notify-osd}}.
+
  
 
===Other DEs===
 
===Other DEs===
Other [[desktop environment|desktop environments]] could use {{Pkg|notification-daemon}} (should be launched explicitly), {{Pkg|xfce4-notifyd}} or {{AUR|notify-osd}} (automatically launched by D-Bus on request).
+
In other desktop environments, the notification server is launched on first call via DBus. You can choose one of the following implementations:
 +
*{{Pkg|xfce4-notifyd}} is a notification server for [[Xfce]], available in the [[Official Repositories]].
 +
:{{Tip|To configure xfce4-notifyd, run the following command in the terminal: {{ic|xfce4-notifyd-config}}.}}
 +
*{{AUR|notify-osd}} is a notification server for [[Unity]], available in the [[Arch User Repository]].
 +
*{{AUR|awn-extras-applets}} contains a notification-daemon applet for the [[Avant Window Navigator]], available in the [[Arch User Repository]].
  
 
==Tips and tricks==
 
==Tips and tricks==
Line 77: Line 80:
 
Notify.uninit ()</nowiki>}}
 
Notify.uninit ()</nowiki>}}
  
====JavaScript (gjs)====
+
====Java====
*Dependencies: {{Pkg|libnotify}}, {{Pkg|gjs}}
+
*Dependency: {{AUR|java-gnome}} from AUR
 +
*Makedependency: java-environment
 +
*Build with: {{ic|mkdir HelloWorld && javac -classpath /usr/share/java/gtk.jar -d HelloWorld HelloWorld.java}}
 +
*Run with: {{ic|java -classpath /usr/share/java/gtk.jar:HelloWorld HelloWorld}}
 +
 
 +
{{hc|HelloWorld.java|<nowiki>import org.gnome.gtk.Gtk;
 +
import org.gnome.notify.Notify;
 +
import org.gnome.notify.Notification;
 +
 
 +
public class HelloWorld
 +
{
 +
    public static void main(String[] args) {
 +
        Gtk.init(args);
 +
        Notify.init("Hello world");
 +
        Notification Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information");
 +
        Hello.show();
 +
        Notify.uninit();
 +
    }
 +
}</nowiki>}}
 +
 
 +
====JavaScript====
 +
*Dependencies: {{Pkg|libnotify}}, {{Pkg|gjs}} (works also with {{Pkg|seed}})
 
{{hc|hello_world.js|<nowiki>#!/usr/bin/gjs
 
{{hc|hello_world.js|<nowiki>#!/usr/bin/gjs
Notify = imports.gi.Notify;
 
Notify.init ("Hello world");
 
Hello=Notify.Notification.new ("Hello world!","This is an example notification.","dialog-information");
 
Hello.show ();
 
Notify.uninit ();</nowiki>}}
 
 
====JavaScript (seed)====
 
*Dependencies: {{Pkg|libnotify}}, {{Pkg|seed}}
 
{{hc|hello_world.js|<nowiki>#!/usr/bin/seed
 
 
Notify = imports.gi.Notify;
 
Notify = imports.gi.Notify;
 
Notify.init ("Hello world");
 
Notify.init ("Hello world");
Line 113: Line 128:
 
Notify.uninit ()</nowiki>}}
 
Notify.uninit ()</nowiki>}}
  
====Python 2 (python-notify)====
+
Or you can use the old, static python-notify bindings:
 
*Dependency: {{Pkg|python-notify}}
 
*Dependency: {{Pkg|python-notify}}
 
{{hc|hello_world.py|<nowiki>#!/usr/bin/python2
 
{{hc|hello_world.py|<nowiki>#!/usr/bin/python2

Revision as of 00:39, 11 January 2012

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary end

Libnotify is an easy way to display desktop notifications and information in a small dialog. It's used in much open source apps like Evolution, Pidgin, etc. It has support for Gtk+ and Qt applications and is desktop independent.

Installation

Libnotify can be installed with the package libnotify, available in the Official Repositories.

In order to use libnotify, you have to install a notification server:

GNOME and KDE

GNOME and KDE use their own implementations to display notifications, and you can't replace them. Their notification servers are started automatically on login to receive notifications from applications via DBus.

  • gnome-shell provides a notification server itself. Notifications are displayed at the bottom of the screen.
  • In GNOME Fallback mode, notification-daemon provides the notification server. Notifications are displayed at the top right corner of the screen.
  • KDE uses knotify4 from package kdebase-runtime to display notifications. Notifications are displayed at the bottom right corner of the screen.

Other DEs

In other desktop environments, the notification server is launched on first call via DBus. You can choose one of the following implementations:

Tip: To configure xfce4-notifyd, run the following command in the terminal: xfce4-notifyd-config.

Tips and tricks

Write your own notify app

You can write your own libnotify display messages easily in many programming languages through GObject-Introspection or bindings, or you can simply use bash.

The following examples display simple a "Hello world" notification.

Bash

hello_world.sh
#!/bin/bash
notify-send 'Hello world!' 'This is an example notification.' --icon=dialog-information

C

  • Dependency: libnotify
  • Build with: gcc -o hello_world `pkg-config --cflags --libs libnotify` hello_world.c
hello_world.c
#include <libnotify/notify.h>
void main () {
	notify_init ("Hello world!");
	NotifyNotification * Hello = notify_notification_new ("Hello world", "This is an example notification.", "dialog-information");
	notify_notification_show (Hello, NULL);
	notify_uninit ();
}

C#

hello_world.cs
using Notifications;
public class HelloWorld {
	static void Main() {
		var Hello = new Notification();
		Hello.Summary  = "Hello world!";
		Hello.Body     = "This is an example notification.";
		Hello.IconName = "dialog-information";
		Hello.Show();
	}
}

Genie

  • Dependency: libnotify
  • Makedependency: vala
  • Build with: valac --pkg libnotify hello_world.gs
hello_world.gs
uses 
	Notify

init
	Notify.init ("Hello world")
	var Hello=new Notification ("Hello world!","This is an example notification.","dialog-information")
	Hello.show ()
	Notify.uninit ()

Java

  • Dependency: java-gnomeAUR from AUR
  • Makedependency: java-environment
  • Build with: mkdir HelloWorld && javac -classpath /usr/share/java/gtk.jar -d HelloWorld HelloWorld.java
  • Run with: java -classpath /usr/share/java/gtk.jar:HelloWorld HelloWorld
HelloWorld.java
import org.gnome.gtk.Gtk;
import org.gnome.notify.Notify;
import org.gnome.notify.Notification;

public class HelloWorld
{
    public static void main(String[] args) {
        Gtk.init(args);
        Notify.init("Hello world");
        Notification Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information");
        Hello.show();
        Notify.uninit();
    }
}

JavaScript

hello_world.js
#!/usr/bin/gjs
Notify = imports.gi.Notify;
Notify.init ("Hello world");
Hello=new Notify.Notification ({summary: "Hello world!",
                                body: "This is an example notification.",
                                "icon-name": "dialog-information"});
Hello.show ();
Notify.uninit ();

Perl

hello_world.pl
#!/usr/bin/perl
use Gtk2::Notify -init, "Hello world";
my $hello = Gtk2::Notify->new("Hello world!", "This is an example notification.", "dialog-information");
$hello->show;

Python

hello_world.py
#!/usr/bin/python
from gi.repository import Notify
Notify.init ("Hello world")
Hello=Notify.Notification.new ("Hello world","This is an example notification.","dialog-information")
Hello.show ()
Notify.uninit ()

Or you can use the old, static python-notify bindings:

hello_world.py
#!/usr/bin/python2
import pynotify
pynotify.init ("Hello world")
Hello=pynotify.Notification ("Hello world!","This is an example notification.","dialog-information")
Hello.show ()
pynotify.uninit ()

Ruby

hello_world.rb
#!/usr/bin/ruby
require 'RNotify'
Notify.init("Hello world")
Hello = Notify::Notification.new("Hello world!", "This is an example notification.", "dialog-information")
Hello.show
Notify.uninit

Vala

  • Dependency: libnotify
  • Makedependency: vala
  • Build with: valac --pkg libnotify hello_world.vala
hello_world.vala
using Notify;
public class HelloWorld {
	static void main () {
		Notify.init ("Hello world");
		var Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information");
		Hello.show ();
		Notify.uninit ();
	}
}

See also