Difference between revisions of "Desktop notifications"

From ArchWiki
Jump to: navigation, search
(Tips & Tricks: Make the install instruction copy-n-paste-able)
(Installation: add Enlightenment, reorganize the list)
(46 intermediate revisions by 16 users not shown)
Line 1: Line 1:
[[Category:Development (English)]]
+
[[Category:Development]]
[[Category: HOWTOs (English)]]
+
[[es:Libnotify]]
 +
[[ru:Libnotify]]
 +
{{Article summary start|Summary}}
 +
{{Article summary text|This article discusses how to install, configure and use libnotify for application development.}}
 +
{{Article summary heading|Related}}
 +
{{Article summary wiki|GTK+}}
 +
{{Article summary wiki|Libcanberra}}
 +
{{Article summary end}}
  
{{i18n|Libnotify}}
+
'''Libnotify''' is an easy way to display desktop notifications and information in a small dialog. It implements the [http://developer.gnome.org/notification-spec/ Desktop Notifications Specification] and it is already used by many open source apps like [[Evolution]], [[Pidgin]], etc. It has support for [[GTK+]] and [[Qt]] applications and is desktop independent.
  
{{Translateme}}
+
==Installation==
  
==Introduction==
+
Libnotify can be installed with the package {{Pkg|libnotify}}, available in the [[official repositories]].
  
Libnotify is an easy way to display desktop notifications and information in a small dialog. It's used in much opensource apps like Evolution, Pidgin, etc. It has support for Gtk+ and Qt applications and is desktop independent.
+
In order to use libnotify, you have to install a notification server:
  
==Installation and Configuration==
+
===Builtin servers===
 +
The following desktop environments 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.
  
pacman -S libnotify
+
* [[Avant Window Navigator]] has a notification-daemon applet provided by {{Pkg|awn-extras-applets}}.
 +
* [[Cinnamon]] provides a notification server itself. Notifications are displayed at the top right corner of the screen.
 +
* [[Enlightenment]] provides a notification server trough its Notification module. Notifications are displayed at the top right corner of the screen.
 +
* [[GNOME]] provides a notification server itself. Notifications are displayed at the bottom of the screen.
 +
* [[GNOME Flashback]] uses {{Pkg|notification-daemon}}. Notifications are displayed at the top right corner of the screen.
 +
* [[KDE]] uses '''knotify4''' from package {{Pkg|kdebase-runtime}} to display notifications. Notifications are displayed at the bottom right corner of the screen.
  
Now to configure libnotify and use libnotify you can use the gnome or xfce4  
+
===Other servers===
 +
In other desktop environments, the notification server is launched on the first call via DBus. You can choose one of the following implementations:
 +
* {{pkg|dunst}} is a minimalistic notification daemon for Linux designed to fit nicely into minimalistic windowmanagers like [[dwm]].
 +
* {{pkg|notification-daemon}} is the notification server used by [[GNOME Flashback]]. It doesn't have a D-Bus service file by default. To use it outside from GNOME Flashback, create the following file:
 +
{{hc|/usr/share/dbus-1/services/org.gnome.Notifications.service|
 +
[D-BUS Service]
 +
Name=org.freedesktop.Notifications
 +
Exec=/usr/lib/notification-daemon-1.0/notification-daemon}}
 +
* {{Pkg|notify-osd}} is a notification server for [[Unity]], available in the official repositories.
 +
* {{AUR|statnot}} is a small, lightweight notification daemon that can output notifications to the root window's title, stdout or FIFO pipes, making it integrate very well with tiling window managers. It's available in the [[Arch User Repository]] or as a [https://github.com/halhen/statnot git repo].
 +
* {{AUR|twmn-git}} is a notification system for tiling window managers. It's available in the [[Arch User Repository]] or as a [https://github.com/sboli/twmn git repo].
 +
* {{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}}.}}
  
Gnome
+
==Tips and tricks==
pacman -S notification-daemon
+
  
For configuring
+
===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.
pacman -S gconf-editor
+
  
then run gconf-editor  and select "/apps/notification-daemon/" . There you can configure the notify widget
+
The following examples display simple a "Hello world" notification.
  
XFCE
+
====Bash====
pacman -S xfce4-notifyd
+
*Dependency: {{Pkg|libnotify}}
pacman -S xfconf
+
{{hc|hello_world.sh|<nowiki>#!/bin/bash
 +
notify-send 'Hello world!' 'This is an example notification.' --icon=dialog-information</nowiki>}}
  
For configuration run
+
====Boo====
 +
*Dependency: {{Pkg|notify-sharp}} ({{Pkg|boo}})
 +
*Makedependency:  {{Pkg|boo}}
 +
*Build with: {{ic|booc hello_world.boo}}
 +
*Run with: {{ic|mono hello_world.exe}} (or {{ic|booi hello_world.boo}})
  
xfce4-notifyd-config
+
{{hc|hello_world.boo|<nowiki>import Notifications from "notify-sharp"
 +
Hello = Notification()
 +
Hello.Summary  = "Hello world!"
 +
Hello.Body    = "This is an example notification."
 +
Hello.IconName = "dialog-information"
 +
Hello.Show()</nowiki>}}
  
==Tips & Tricks==
+
====C====
 +
*Dependency: {{Pkg|libnotify}}
 +
*Build with: {{ic|gcc -o hello_world `pkg-config --cflags --libs libnotify` hello_world.c}}
 +
{{hc|hello_world.c|<nowiki>#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);
 +
}</nowiki>}}
  
You can write your own libnotify display messages easily in Python or other languages. Here is a simple Python example.
+
====C++====
 +
*Dependency: {{AUR|libnotifymm}} from AUR
 +
*Build with: {{Ic|g++ -o hello_world `pkg-config --cflags --libs libnotifymm-1.0` hello_world.cc}}
 +
{{hc|hello_world.cc|<nowiki>#include <libnotifymm.h>
 +
int main(int argc, char *argv[]) {
 +
Notify::init("Hello world!");
 +
Notify::Notification Hello("Hello world", "This is an example notification.", "dialog-information");
 +
        Hello.show();
 +
}</nowiki>}}
  
Note you will need to install the Python bindings for libnotify
+
====C#====
 +
*Dependency: {{Pkg|notify-sharp}}
 +
*Build with: {{ic|mcs -pkg:notify-sharp hello_world.cs}}
 +
*Run with: {{ic|mono hello_world.exe}}
 +
{{hc|hello_world.cs|<nowiki>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();
 +
}
 +
}</nowiki>}}
  
pacman -S python-notify  #(communitiy)
+
====Genie====
 +
*Dependency: {{Pkg|libnotify}}
 +
*Makedependency: {{Pkg|vala}}
 +
*Build with: {{ic|valac --pkg libnotify hello_world.gs}}
 +
{{hc|hello_world.gs|<nowiki>uses
 +
Notify
  
Hello world example
+
init
+
Notify.init ("Hello world")
#!/usr/bin/env python
+
var Hello=new Notification ("Hello world!","This is an example notification.","dialog-information")
import subprocess
+
Hello.show ()</nowiki>}}
info = "Hello world "
+
subprocess.call(('notify-send',info))
+
  
#!/usr/bin/python
+
====Java====
import subprocess
+
*Dependency: {{AUR|java-gnome}} from AUR
import commands   
+
*Makedependency: java-environment
#KERNEL VERSION
+
*Build with: {{ic|mkdir HelloWorld && javac -classpath /usr/share/java/gtk.jar -d HelloWorld HelloWorld.java}}
uname = commands.getoutput('uname -r')
+
*Run with: {{ic|java -classpath /usr/share/java/gtk.jar:HelloWorld HelloWorld}}
head = "All the info about your system:"
+
msg = "Your kernel version: "+ uname +"\n"     
+
# print message
+
subprocess.call(['notify-send', head, msg])
+
  
Or you can just use bash
+
{{hc|HelloWorld.java|<nowiki>import org.gnome.gtk.Gtk;
 +
import org.gnome.notify.Notify;
 +
import org.gnome.notify.Notification;
  
# send a notification saying hello world
+
public class HelloWorld
notify-send "hello world"
+
{
 +
    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();
 +
    }
 +
}</nowiki>}}
  
==More Resources==  
+
====JavaScript====
[http://www.florijan.net/2009/05/22/howto-using-python-to-display-notifications-using-libnotify/ Libnotify python example]
+
*Dependencies: {{Pkg|libnotify}}, {{Pkg|gjs}} (works also with {{Pkg|seed}})
[http://roscidus.com/desktop/node/336 another libnotify example]
+
{{hc|hello_world.js|<nowiki>#!/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 ();</nowiki>}}
 +
 
 +
====Perl====
 +
*Dependencies: {{Pkg|libnotify}}, {{AUR|perl-glib-object-introspection}} from AUR
 +
{{hc|hello_world.pl|<nowiki>#!/usr/bin/perl
 +
use Glib::Object::Introspection;
 +
Glib::Object::Introspection->setup (
 +
basename => 'Notify',
 +
version => '0.7',
 +
package => 'Notify');
 +
Notify->init;
 +
my $hello = Notify::Notification->new("Hello world!", "This is an example notification.", "dialog-information");
 +
$hello->show;</nowiki>}}
 +
 
 +
Or you can use the old, static perl-gtk2-notify bindings:
 +
*Dependency: {{AUR|perl-gtk2-notify}} from AUR
 +
{{hc|hello_world.pl|<nowiki>#!/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;</nowiki>}}
 +
 
 +
====Python====
 +
*Dependencies: {{Pkg|libnotify}}, {{Pkg|python-gobject}}
 +
{{hc|hello_world.py|<nowiki>#!/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 ()</nowiki>}}
 +
 
 +
Or you can use the old, static python-notify bindings:
 +
*Dependency: {{Pkg|python2-notify}}
 +
{{hc|hello_world.py|<nowiki>#!/usr/bin/python2
 +
import pynotify
 +
pynotify.init ("Hello world")
 +
Hello=pynotify.Notification ("Hello world!","This is an example notification.","dialog-information")
 +
Hello.show ()</nowiki>}}
 +
 
 +
====Ruby====
 +
*Dependencies: {{Pkg|libnotify}}, {{AUR|ruby-gir-ffi}} from AUR
 +
{{hc|hello_world.rb|<nowiki>#!/usr/bin/ruby
 +
require 'gir_ffi'
 +
GirFFI.setup :Notify
 +
Notify.init("Hello world")
 +
Hello = Notify::Notification.new("Hello world!", "This is an example notification.", "dialog-information")
 +
Hello.show</nowiki>}}
 +
 
 +
Or you can use the old, static ruby-libnotify bindings:
 +
*Dependency: {{AUR|ruby-libnotify}} from AUR
 +
{{hc|hello_world.rb|<nowiki>#!/usr/bin/ruby
 +
require 'RNotify'
 +
Notify.init("Hello world")
 +
Hello = Notify::Notification.new("Hello world!", "This is an example notification.", "dialog-information")
 +
Hello.show</nowiki>}}
 +
 
 +
====Vala====
 +
*Dependency: {{Pkg|libnotify}}
 +
*Makedependency: {{Pkg|vala}}
 +
*Build with: {{ic|valac --pkg libnotify hello_world.vala}}
 +
{{hc|hello_world.vala|<nowiki>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 ();
 +
}
 +
}</nowiki>}}
 +
 
 +
====Visual Basic .NET====
 +
*Dependency: {{Pkg|notify-sharp}}
 +
*Makedependency: {{Pkg|mono-basic}}
 +
*Build with: {{ic|vbnc -r:/usr/lib/mono/notify-sharp/notify-sharp.dll hello_world.vb}}
 +
*Run with: {{ic|mono hello_world.exe}}
 +
 
 +
{{hc|hello_world.vb|<nowiki>Imports Notifications
 +
Public Class Hello
 +
Public Shared Sub Main
 +
Dim Hello As New Notification
 +
Hello.Summary  = "Hello world!"
 +
Hello.Body    = "This is an example notification."
 +
Hello.IconName = "dialog-information"
 +
Hello.Show
 +
End Sub
 +
End Class</nowiki>}}
 +
 
 +
==See also==
 +
*[http://developer.gnome.org/libnotify/ Libnotify Reference Manual]
 +
*[http://milky.manishsinha.net/2009/03/29/working-with-libnotify/ C example]
 +
*[http://hashbang.fr/tutoriel-notify.html Python example] (french article)

Revision as of 14:48, 31 August 2013

Summary help replacing me
This article discusses how to install, configure and use libnotify for application development.
Related
GTK+
Libcanberra

Libnotify is an easy way to display desktop notifications and information in a small dialog. It implements the Desktop Notifications Specification and it is already used by many 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:

Builtin servers

The following desktop environments 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.

  • Avant Window Navigator has a notification-daemon applet provided by awn-extras-applets.
  • Cinnamon provides a notification server itself. Notifications are displayed at the top right corner of the screen.
  • Enlightenment provides a notification server trough its Notification module. Notifications are displayed at the top right corner of the screen.
  • GNOME provides a notification server itself. Notifications are displayed at the bottom of the screen.
  • GNOME Flashback uses notification-daemon. 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 servers

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

  • dunst is a minimalistic notification daemon for Linux designed to fit nicely into minimalistic windowmanagers like dwm.
  • notification-daemon is the notification server used by GNOME Flashback. It doesn't have a D-Bus service file by default. To use it outside from GNOME Flashback, create the following file:
/usr/share/dbus-1/services/org.gnome.Notifications.service
[D-BUS Service]
Name=org.freedesktop.Notifications
Exec=/usr/lib/notification-daemon-1.0/notification-daemon
  • notify-osd is a notification server for Unity, available in the official repositories.
  • statnotAUR is a small, lightweight notification daemon that can output notifications to the root window's title, stdout or FIFO pipes, making it integrate very well with tiling window managers. It's available in the Arch User Repository or as a git repo.
  • twmn-gitAUR is a notification system for tiling window managers. It's available in the Arch User Repository or as a git repo.
  • 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: 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

Boo

  • Dependency: notify-sharp (boo)
  • Makedependency: boo
  • Build with: booc hello_world.boo
  • Run with: mono hello_world.exe (or booi hello_world.boo)
hello_world.boo
import Notifications from "notify-sharp"
Hello = Notification()
Hello.Summary  = "Hello world!"
Hello.Body     = "This is an example notification."
Hello.IconName = "dialog-information"
Hello.Show()

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);
}

C++

  • Dependency: libnotifymmAUR from AUR
  • Build with: g++ -o hello_world `pkg-config --cflags --libs libnotifymm-1.0` hello_world.cc
hello_world.cc
#include <libnotifymm.h>
int main(int argc, char *argv[]) {
	Notify::init("Hello world!");
	Notify::Notification Hello("Hello world", "This is an example notification.", "dialog-information");
        Hello.show();
}

C#

  • Dependency: notify-sharp
  • Build with: mcs -pkg:notify-sharp hello_world.cs
  • Run with: mono hello_world.exe
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 ()

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();
    }
}

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 ();

Perl

hello_world.pl
#!/usr/bin/perl
use Glib::Object::Introspection;
Glib::Object::Introspection->setup (
	basename => 'Notify',
	version => '0.7',
	package => 'Notify');
Notify->init;
my $hello = Notify::Notification->new("Hello world!", "This is an example notification.", "dialog-information");
$hello->show;

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

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 ()

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 ()

Ruby

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

Or you can use the old, static ruby-libnotify bindings:

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

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 ();
	}
}

Visual Basic .NET

  • Dependency: notify-sharp
  • Makedependency: mono-basic
  • Build with: vbnc -r:/usr/lib/mono/notify-sharp/notify-sharp.dll hello_world.vb
  • Run with: mono hello_world.exe
hello_world.vb
Imports Notifications
Public Class Hello
	Public Shared Sub Main
		Dim Hello As New Notification
		Hello.Summary  = "Hello world!"
		Hello.Body     = "This is an example notification."
		Hello.IconName = "dialog-information"
		Hello.Show
	End Sub
End Class

See also