Difference between revisions of "Desktop notifications"

From ArchWiki
Jump to: navigation, search
(Python: mention Python 2 bindings)
(Undo revision 458487 by Lahwaacz.bot (talk) libnotifymm is a working package, added back to AUR.)
 
(80 intermediate revisions by 24 users not shown)
Line 1: Line 1:
 +
[[Category:X server]]
 
[[Category:Development]]
 
[[Category:Development]]
[[es:Libnotify]]
+
[[es:Desktop notifications]]
[[ru:Libnotify]]
+
[[ja:デスクトップ通知]]
{{Article summary start|Summary}}
+
[[ru:Desktop notifications]]
{{Article summary text|This article discusses how to install, configure and use libnotify for application development.}}
+
{{Related articles start}}
{{Article summary heading|Related}}
+
{{Related|GTK+}}
{{Article summary wiki|GTK+}}
+
{{Related|Libcanberra}}
{{Article summary wiki|Libcanberra}}
+
{{Related articles end}}
{{Article summary end}}
+
Desktop notifications are small, passive popup dialogs that notify the user of particular events in an asynchronous manner.
  
'''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.
+
==Libnotify==
  
==Installation==
+
Libnotify is an implementation of the [http://developer.gnome.org/notification-spec/ Desktop Notifications Specification] which provides support for [[GTK+]] and [[Qt]] applications and is desktop independent: it is already used by many open source apps like [[Evolution]] and [[Pidgin]]. Libnotify can be installed with the {{Pkg|libnotify}} package.
  
Libnotify can be installed with the package {{Pkg|libnotify}}, available in the [[official repositories]].
+
In order to use libnotify, you have to install a [[#Notification servers|notification server]].
  
In order to use libnotify, you have to install a notification server:
+
== Notification servers ==
  
===Builtin servers===
+
===Built-in===
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.
+
The following desktop environments use their own implementations to display notifications, and you cannot 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 {{Pkg|awn-extras-applets}}.
 
 
* [[Cinnamon]] provides a notification server itself. Notifications are displayed at the top right corner of the screen.
 
* [[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.
+
* [[Enlightenment]] provides a notification server through its Notification extension. Notification options [[Enlightenment#Notifications|are configurable]].
* [[GNOME]] provides a notification server itself. Notifications are displayed at the bottom of the screen.
+
* [[GNOME]] provides a notification server itself. Notifications are displayed at the top of the screen.
* [[GNOME Flashback]] uses {{Pkg|notification-daemon}}. Notifications are displayed at the top right corner of the screen.
+
* [[KDE Plasma]] provides a notification server itself. Notifications are displayed at the bottom 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.
+
  
===Other servers===
+
===Standalone===
In other desktop environments, the notification server is launched on the first call via DBus. You can choose one of the following implementations:
+
In other desktop environments, the notification server needs to be launched using your WM's/DE's "autostart" option. (It ''can'' be launched on the first call via DBus, but that's not very desirable as it needs global configuration.)
* {{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}}.}}
+
  
==Tips and tricks==
+
You can choose one of the following implementations:
  
===Write your own notify app===
+
* {{App|[[Avant Window Navigator]]|A notification-daemon applet is available for AWN.|https://github.com/p12tic/awn-extras|{{AUR|awn-extras-applets}}}}
 +
* {{App|Deepin Notifications|Notification server for [[Deepin]].|https://github.com/linuxdeepin/deepin-notifications|{{pkg|deepin-notifications}}}}
 +
* {{App|Dunst|Minimalistic notification daemon for Linux designed to fit nicely into minimalistic windowmanagers like [[dwm]].|http://www.knopwob.org/dunst/|{{pkg|dunst}}}}
 +
* {{App|LXQt Notification Daemon|Notification server for [[LXQt]].|https://github.com/lxde/lxqt-notificationd|{{pkg|lxqt-notificationd}}}}
 +
* {{App|Notification Daemon|The notification server used by [[GNOME Flashback]].|https://github.com/GNOME/notification-daemon|{{pkg|notification-daemon}}}}
 +
:You can run it manually using {{ic|/usr/lib/notification-daemon-1.0/notification-daemon}}.
 +
* {{App|MATE Notification Daemon|Notification server for [[MATE]].|https://github.com/mate-desktop/mate-notification-daemon/|GTK+ 2: {{Pkg|mate-notification-daemon}}, GTK+ 3 (experimental): {{Pkg|mate-notification-daemon-gtk3}}{{Broken package link|replaced by {{Pkg|mate-notification-daemon}}}}}}
 +
* {{App|Notify OSD|Notification server for [[Unity]].|https://launchpad.net/notify-osd|{{Pkg|notify-osd}}}}
 +
* {{App|statnot|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.|https://github.com/halhen/statnot|{{AUR|statnot}}}}
 +
* {{App|twmn|Notification system for tiling window managers.|https://github.com/sboli/twmn|{{AUR|twmn-git}}}}
 +
* {{App|Xfce Notification Daemon|Notification server for [[Xfce]].|http://goodies.xfce.org/projects/applications/xfce4-notifyd|{{Pkg|xfce4-notifyd}}}}
 +
:{{Tip|To configure xfce4-notifyd, run the following command: {{ic|xfce4-notifyd-config}}.}}
 +
 
 +
==Usage in programming==
 
You can write your own libnotify display messages easily in many programming languages through GObject-Introspection or bindings, or you can simply use bash.
 
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.
 
The following examples display simple a "Hello world" notification.
  
====Bash====
+
'''Bash'''
*Dependency: {{Pkg|libnotify}}
+
 
{{hc|hello_world.sh|<nowiki>#!/bin/bash
+
* Dependency: {{Pkg|libnotify}}
notify-send 'Hello world!' 'This is an example notification.' --icon=dialog-information</nowiki>}}
+
 
 +
{{hc|hello_world.sh|2=
 +
#!/bin/bash
 +
notify-send 'Hello world!' 'This is an example notification.' --icon=dialog-information
 +
}}
 +
 
 +
{{Tip|
 +
* An overview on the available icons can be found [http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html here].
 +
* To send desktop notification from a background script running as root (replace {{ic|''X_user''}} and {{ic|''X_userid''}} with the user and userid running X respectively): {{bc|1=# sudo -u ''X_user'' DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/''X_userid''/bus notify-send 'Hello world!' 'This is an example notification.'}}
 +
}}
 +
 
 +
'''Boo'''
  
====Boo====
+
* Dependency: {{Pkg|notify-sharp-3}} ({{Pkg|boo}})
*Dependency: {{Pkg|notify-sharp}} ({{Pkg|boo}})
+
* Makedependency:  {{Pkg|boo}}
*Makedependency:  {{Pkg|boo}}
+
* Build with: {{ic|booc hello_world.boo}}
*Build with: {{ic|booc hello_world.boo}}
+
* Run with: {{ic|mono hello_world.exe}} (or {{ic|booi hello_world.boo}})
*Run with: {{ic|mono hello_world.exe}} (or {{ic|booi hello_world.boo}})
+
  
{{hc|hello_world.boo|<nowiki>import Notifications from "notify-sharp"
+
{{hc|hello_world.boo|2=
 +
import Notifications from "notify-sharp"
 
Hello = Notification()
 
Hello = Notification()
 
Hello.Summary  = "Hello world!"
 
Hello.Summary  = "Hello world!"
 
Hello.Body    = "This is an example notification."
 
Hello.Body    = "This is an example notification."
 
Hello.IconName = "dialog-information"
 
Hello.IconName = "dialog-information"
Hello.Show()</nowiki>}}
+
Hello.Show()
 +
}}
  
====C====
+
'''C'''
*Dependency: {{Pkg|libnotify}}
+
 
*Build with: {{ic|gcc -o hello_world `pkg-config --cflags --libs libnotify` hello_world.c}}
+
* Dependency: {{Pkg|libnotify}}
{{hc|hello_world.c|<nowiki>#include <libnotify/notify.h>
+
* Build with: {{ic|gcc -o hello_world `pkg-config --cflags --libs libnotify` hello_world.c}}
void main () {
+
 
 +
{{hc|hello_world.c|2=
 +
#include <libnotify/notify.h>
 +
int main() {
 
notify_init ("Hello world!");
 
notify_init ("Hello world!");
 
NotifyNotification * Hello = notify_notification_new ("Hello world", "This is an example notification.", "dialog-information");
 
NotifyNotification * Hello = notify_notification_new ("Hello world", "This is an example notification.", "dialog-information");
 
notify_notification_show (Hello, NULL);
 
notify_notification_show (Hello, NULL);
}</nowiki>}}
+
g_object_unref(G_OBJECT(Hello));
 +
notify_uninit();
 +
return 0;
 +
}
 +
}}
  
====C++====
+
'''C++'''
*Dependency: {{AUR|libnotifymm}} from AUR
+
 
*Build with: {{Ic|g++ -o hello_world `pkg-config --cflags --libs libnotifymm-1.0` hello_world.cc}}
+
* Dependency: {{AUR|libnotifymm}}
{{hc|hello_world.cc|<nowiki>#include <libnotifymm.h>
+
* Build with: {{Ic|g++ -o hello_world `pkg-config --cflags --libs libnotifymm-1.0` hello_world.cc}}
 +
 
 +
{{hc|hello_world.cc|2=
 +
#include <libnotifymm.h>
 
int main(int argc, char *argv[]) {
 
int main(int argc, char *argv[]) {
 
Notify::init("Hello world!");
 
Notify::init("Hello world!");
 
Notify::Notification Hello("Hello world", "This is an example notification.", "dialog-information");
 
Notify::Notification Hello("Hello world", "This is an example notification.", "dialog-information");
        Hello.show();
+
Hello.show();
}</nowiki>}}
+
return 0;
 +
}
 +
}}
  
====C#====
+
'''C#'''
*Dependency: {{Pkg|notify-sharp}}
+
 
*Build with: {{ic|mcs -pkg:notify-sharp hello_world.cs}}
+
* Dependency: {{Pkg|notify-sharp-3}}
*Run with: {{ic|mono hello_world.exe}}
+
* Build with: {{ic|mcs -pkg:notify-sharp-3.0 hello_world.cs}}
{{hc|hello_world.cs|<nowiki>using Notifications;
+
* Run with: {{ic|mono hello_world.exe}}
 +
 
 +
{{hc|hello_world.cs|2=
 +
using Notifications;
 
public class HelloWorld {
 
public class HelloWorld {
 
static void Main() {
 
static void Main() {
Line 99: Line 127:
 
Hello.Show();
 
Hello.Show();
 
}
 
}
}</nowiki>}}
+
}
 +
}}
  
====Genie====
+
'''Cobra'''
*Dependency: {{Pkg|libnotify}}
+
 
*Makedependency: {{Pkg|vala}}
+
* Dependency: {{Pkg|notify-sharp-3}}
*Build with: {{ic|valac --pkg libnotify hello_world.gs}}
+
* Makedependency: {{AUR|cobra}}
{{hc|hello_world.gs|<nowiki>uses  
+
* Build with: {{ic|cobra -c hello_world}}
 +
* Run with: {{ic|mono hello_world.exe}}
 +
 
 +
{{hc|hello_world.cobra|<nowiki>@args -pkg:notify-sharp-3.0
 +
use Notifications
 +
class HelloWorld
 +
    def main
 +
        hello = Notification()
 +
        hello.summary  = "Hello world!"
 +
        hello.body    = "This is an example notification."
 +
        hello.iconName = "dialog-information"
 +
        hello.show</nowiki>}}
 +
 
 +
'''F#'''
 +
 
 +
* Dependency: {{Pkg|notify-sharp-3}}
 +
* Makedependency:  {{AUR|fsharp}}
 +
* Build with: {{ic|fsharpc -r:notify-sharp.dll -I:/usr/lib/mono/notify-sharp-3.0/ -I:/usr/lib/mono/gtk-sharp-3.0/ hello_world.fs}}
 +
* Run with: {{ic|mono hello_world.exe}}
 +
 
 +
{{hc|hello_world.fs|2=
 +
open Notifications
 +
let Hello = new Notification()
 +
Hello.Summary  <- "Hello world!"
 +
Hello.Body    <- "This is an example notification."
 +
Hello.IconName <- "dialog-information"
 +
Hello.Show()
 +
}}
 +
 
 +
'''Genie'''
 +
 
 +
* Dependency: {{Pkg|libnotify}}
 +
* Makedependency: {{Pkg|vala}}
 +
* Build with: {{ic|valac --pkg libnotify hello_world.gs}}
 +
 
 +
{{hc|hello_world.gs|2=
 +
uses  
 
Notify
 
Notify
  
 
init
 
init
 
Notify.init ("Hello world")
 
Notify.init ("Hello world")
var Hello=new Notification ("Hello world!","This is an example notification.","dialog-information")
+
var Hello=new Notify.Notification ("Hello world!","This is an example notification.","dialog-information")
Hello.show ()</nowiki>}}
+
Hello.show ()
 +
}}
 +
 
 +
'''Go'''
 +
 
 +
* Dependency: {{Pkg|libnotify}}
 +
* Makedependency: {{AUR|go-notify-git}}
 +
* Build with: {{ic|go build hello_world.go}}
 +
* (Or run with: {{ic|go run hello_world.go}})
 +
 
 +
{{hc|hello_world.go|2=
 +
package main
 +
import ("github.com/mqu/go-notify")
 +
 
 +
func main() {
 +
notify.Init("Hello world")
 +
hello := notify.NotificationNew("Hello World!", "This is an example notification.","dialog-information")
 +
hello.Show()
 +
}
 +
}}
 +
 
 +
'''Groovy'''
 +
 
 +
* Dependencies: {{Pkg|groovy}}, {{AUR|java-gnome}}
 +
* Build with: {{ic|groovyc -cp /usr/share/java/gtk.jar HelloWorld.groovy && jar cfe HelloWorld.jar HelloWorld HelloWorld.class}}
 +
* Run with: {{ic|java -cp /usr/share/groovy/embeddable/groovy-all.jar:/usr/share/java/gtk.jar:HelloWorld.jar HelloWorld}} or {{ic|groovy -cp /usr/share/java/gtk.jar HelloWorld.groovy}}
 +
 
 +
{{hc|HelloWorld.groovy|2=
 +
import org.gnome.gtk.*
 +
import org.gnome.notify.*
 +
 
 +
Gtk.init()
 +
Notify.init("Hello world")
 +
def Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information")
 +
Hello.show()
 +
}}
 +
 
 +
'''Haskell'''
 +
 
 +
* Makedependency: {{Pkg|haskell-fdo-notify}}
 +
* Build with: {{ic|ghc hello_world}}
 +
{{hc|hello_world.hs|2=
 +
import DBus.Notify
 +
main = do
 +
        client <- connectSession
 +
        let hello = blankNote { summary="Hello world!",
 +
                                body=(Just $ Text "This is an example notification."),
 +
                                appImage=(Just $ Icon "dialog-information") }
 +
        notification <- notify client hello
 +
        return 0
 +
}}
 +
 
 +
'''IronPython'''
 +
 
 +
* Dependencies: {{Pkg|notify-sharp-3}}, {{Pkg|ironpython}}
 +
* Run with: {{ic|ipy hello_world.py}}
 +
 
 +
{{hc|hello_world.py|2=
 +
import clr
 +
clr.AddReference('notify-sharp')
 +
import Notifications
 +
Hello = Notifications.Notification()
 +
Hello.Summary  = "Hello world!"
 +
Hello.Body    = "This is an example notification."
 +
Hello.IconName = "dialog-information"
 +
Hello.Show()
 +
}}
 +
 
 +
'''Java'''
  
====Java====
+
* Dependency: {{AUR|java-gnome}}
*Dependency: {{AUR|java-gnome}} from AUR
+
* Makedependency: java-environment
*Makedependency: java-environment
+
* Build with: {{ic|javac -cp /usr/share/java/gtk.jar HelloWorld.java && jar cfe HelloWorld.jar HelloWorld HelloWorld.class}}
*Build with: {{ic|mkdir HelloWorld && javac -classpath /usr/share/java/gtk.jar -d HelloWorld HelloWorld.java}}
+
* Run with: {{ic|java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld}}
*Run with: {{ic|java -classpath /usr/share/java/gtk.jar:HelloWorld HelloWorld}}
+
  
{{hc|HelloWorld.java|<nowiki>import org.gnome.gtk.Gtk;
+
{{hc|HelloWorld.java|2=
 +
import org.gnome.gtk.Gtk;
 
import org.gnome.notify.Notify;
 
import org.gnome.notify.Notify;
 
import org.gnome.notify.Notification;
 
import org.gnome.notify.Notification;
Line 131: Line 264:
 
         Hello.show();
 
         Hello.show();
 
     }
 
     }
}</nowiki>}}
+
}
 +
}}
  
====JavaScript====
+
'''JavaScript'''
*Dependencies: {{Pkg|libnotify}}, {{Pkg|gjs}} (works also with {{Pkg|seed}})
+
 
{{hc|hello_world.js|<nowiki>#!/usr/bin/gjs
+
* Dependencies: {{Pkg|libnotify}}, {{Pkg|gjs}}
Notify = imports.gi.Notify;
+
 
 +
{{hc|hello_world.js|2=
 +
#!/usr/bin/gjs
 +
const Notify = imports.gi.Notify;
 
Notify.init ("Hello world");
 
Notify.init ("Hello world");
Hello=new Notify.Notification ({summary: "Hello world!",
+
var Hello=new Notify.Notification ({summary: "Hello world!",
                                body: "This is an example notification.",
+
                                    body: "This is an example notification.",
                                "icon-name": "dialog-information"});
+
                                    "icon-name": "dialog-information"});
Hello.show ();</nowiki>}}
+
Hello.show ();
 +
}}
 +
 
 +
'''JRuby'''
 +
 
 +
* Dependencies: {{AUR|java-gnome}}, {{Pkg|jruby}}
 +
* Build with: {{ic|jrubyc hello_world.rb && jar cfe hello_world.jar hello_world hello_world.class}}
 +
* Run with: {{ic|java -cp /opt/jruby/lib/jruby.jar:hello_world.jar hello_world}} or {{ic|jruby hello_world.rb}}
 +
{{hc|hello_world.rb|2=
 +
require '/usr/share/java/gtk.jar'
 +
import Java::OrgGnomeGtk::Gtk
 +
import Java::OrgGnomeNotify::Notify
 +
import Java::OrgGnomeNotify::Notification
 +
 
 +
Gtk.init(nil)
 +
Notify.init("Hello world")
 +
Hello = Notification.new("Hello world!", "This is an example notification.", "dialog-information")
 +
Hello.show
 +
}}
 +
 
 +
'''Jython'''
 +
 
 +
* Dependencies: {{AUR|java-gnome}}, {{Pkg|jython}}
 +
* Run with: {{ic|1=jython -Dpython.path=/usr/share/java/gtk.jar hello_world.py}}
 +
{{hc|hello_world.py|2=
 +
from org.gnome.gtk import Gtk
 +
from org.gnome.notify import Notify, Notification
 +
Gtk.init(None)
 +
Notify.init("Hello world")
 +
Hello=Notification("Hello world!", "This is an example notification.", "dialog-information")
 +
Hello.show()
 +
}}
 +
 
 +
'''Lua'''
 +
 
 +
* Dependencies: {{Pkg|libnotify}}, {{Pkg|lua-lgi}}
 +
 
 +
{{hc|hello_world.lua|2=
 +
#!/usr/bin/lua
 +
lgi = require 'lgi'
 +
Notify = lgi.require('Notify')
 +
Notify.init("Hello world")
 +
Hello=Notify.Notification.new("Hello world","This is an example notification.","dialog-information")
 +
Hello:show()
 +
}}
 +
 
 +
'''Nemerle'''
 +
 
 +
* Dependency: {{Pkg|notify-sharp-3}}
 +
* Makedependency: {{AUR|nemerle}}
 +
* Build with: {{ic|ncc -pkg:notify-sharp-3.0 -out:hello_world.exe hello_world.n}}
 +
* Run with: {{ic|mono hello_world.exe}}
 +
 
 +
{{hc|hello_world.n|2=
 +
using Notifications;
 +
public class HelloWorld {
 +
static Main() : void {
 +
def Hello = Notification();
 +
Hello.Summary  = "Hello world!";
 +
Hello.Body    = "This is an example notification.";
 +
Hello.IconName = "dialog-information";
 +
Hello.Show();
 +
}
 +
}
 +
}}
 +
 
 +
'''Pascal'''
 +
 
 +
* Dependency: {{Pkg|libnotify}}
 +
* Makedependency: {{Pkg|fpc}}, [https://github.com/ik5/libnotify-fpc libnotify binding]
 +
* Build with: {{ic|fpc hello_world}}
 +
 
 +
{{hc|hello_world.pas|2=
 +
program hello_world;
 +
uses libnotify;
 +
var hello : PNotifyNotification;
 +
begin
 +
notify_init(argv[0]);
 +
hello := notify_notification_new ('Hello world', 'This is an example notification.', 'dialog-information');
 +
notify_notification_show (hello, nil);
 +
end.
 +
}}
 +
 
 +
'''Perl'''
  
====Perl====
+
* Dependencies: {{Pkg|libnotify}}, {{AUR|perl-glib-object-introspection}}
*Dependencies: {{Pkg|libnotify}}, {{AUR|perl-glib-object-introspection}} from AUR
+
{{hc|hello_world.pl|2=
{{hc|hello_world.pl|<nowiki>#!/usr/bin/perl
+
#!/usr/bin/perl
 
use Glib::Object::Introspection;
 
use Glib::Object::Introspection;
 
Glib::Object::Introspection->setup (
 
Glib::Object::Introspection->setup (
Line 153: Line 373:
 
Notify->init;
 
Notify->init;
 
my $hello = Notify::Notification->new("Hello world!", "This is an example notification.", "dialog-information");
 
my $hello = Notify::Notification->new("Hello world!", "This is an example notification.", "dialog-information");
$hello->show;</nowiki>}}
+
$hello->show;
 +
}}
  
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====
+
'''Python'''
*Dependencies: {{Pkg|libnotify}}, {{Pkg|python-gobject}} (or {{Pkg|python2-gobject}} for Python 2)
+
 
{{hc|hello_world.py|<nowiki>#!/usr/bin/python
+
* Dependencies: {{Pkg|libnotify}}, {{Pkg|python-gobject}} (or {{Pkg|python2-gobject}} for Python 2)
 +
 
 +
{{hc|hello_world.py|2=
 +
#!/usr/bin/python
 +
import gi
 +
gi.require_version('Notify', '0.7')
 
from gi.repository import Notify
 
from gi.repository import Notify
Notify.init ("Hello world")
+
Notify.init("Hello world")
Hello=Notify.Notification.new ("Hello world","This is an example notification.","dialog-information")
+
Hello=Notify.Notification.new("Hello world", "This is an example notification.", "dialog-information")
Hello.show ()</nowiki>}}
+
Hello.show()
 +
}}
  
Or you can use the old, static python-notify bindings:
+
'''Ruby'''
*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}}
*Dependencies: {{Pkg|libnotify}}, {{AUR|ruby-gir-ffi}} from AUR
+
 
{{hc|hello_world.rb|<nowiki>#!/usr/bin/ruby
+
{{hc|hello_world.rb|2=
 +
#!/usr/bin/ruby
 
require 'gir_ffi'
 
require 'gir_ffi'
 
GirFFI.setup :Notify
 
GirFFI.setup :Notify
 
Notify.init("Hello world")
 
Notify.init("Hello world")
 
Hello = Notify::Notification.new("Hello world!", "This is an example notification.", "dialog-information")
 
Hello = Notify::Notification.new("Hello world!", "This is an example notification.", "dialog-information")
Hello.show</nowiki>}}
+
Hello.show
 +
}}
  
Or you can use the old, static ruby-libnotify bindings:
+
'''Rust'''
*Dependency: {{AUR|ruby-libnotify}} from AUR
+
 
{{hc|hello_world.rb|<nowiki>#!/usr/bin/ruby
+
Using [https://crates.io/crates/notify-rust notify-rust].
require 'RNotify'
+
* Makedependency: {{Pkg|cargo}}
Notify.init("Hello world")
+
* Build with: {{ic|cargo build}}
Hello = Notify::Notification.new("Hello world!", "This is an example notification.", "dialog-information")
+
* Run with: {{ic|target/debug/hello_world}} or {{ic|cargo run}}
Hello.show</nowiki>}}
+
 
 +
{{hc|Cargo.toml|2=
 +
[package]
 +
name = "hello_world"
 +
version = "0.1.0"
 +
 
 +
[dependencies]
 +
notify-rust = "^3"
 +
}}
 +
 
 +
{{hc|src/main.rs|2=
 +
extern crate notify_rust;
 +
use notify_rust::Notification;
 +
fn main(){
 +
    Notification::new()
 +
        .summary("Hello world")
 +
        .body("This is an example notification.")
 +
        .icon("dialog-information")
 +
        .show().unwrap();
 +
}
 +
}}
 +
 
 +
'''Scala'''
 +
 
 +
* Dependency: {{AUR|java-gnome}} (and {{Pkg|scala}})
 +
* Makedependency: {{Pkg|scala}}
 +
* Build with: {{ic|scalac -cp /usr/share/java/gtk.jar -d HelloWorld.jar HelloWorld.scala}}
 +
* Run with: {{ic|java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld}} (or {{ic|scala -cp /usr/share/java/gtk.jar HelloWorld.scala}})
 +
 
 +
{{hc|HelloWorld.scala|2=
 +
import org.gnome.gtk._
 +
import org.gnome.notify._
 +
 
 +
object HelloWorld {
 +
  def main(args: Array[String]) {
 +
    Gtk.init(args)
 +
    Notify.init("Hello world")
 +
    var Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information")
 +
    Hello.show()
 +
  }
 +
}
 +
}}
 +
 
 +
'''Vala'''
  
====Vala====
 
 
*Dependency: {{Pkg|libnotify}}
 
*Dependency: {{Pkg|libnotify}}
 
*Makedependency: {{Pkg|vala}}
 
*Makedependency: {{Pkg|vala}}
 
*Build with: {{ic|valac --pkg libnotify hello_world.vala}}
 
*Build with: {{ic|valac --pkg libnotify hello_world.vala}}
{{hc|hello_world.vala|<nowiki>using Notify;
+
 
 +
{{hc|hello_world.vala|2=
 +
using Notify;
 
public class HelloWorld {
 
public class HelloWorld {
 
static void main () {
 
static void main () {
 
Notify.init ("Hello world");
 
Notify.init ("Hello world");
var Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information");
+
var Hello = new Notify.Notification("Hello world!", "This is an example notification.", "dialog-information");
 
Hello.show ();
 
Hello.show ();
 
}
 
}
}</nowiki>}}
+
}
 +
}}
 +
 
 +
'''Visual Basic .NET'''
  
====Visual Basic .NET====
+
* Dependency: {{Pkg|notify-sharp-3}}
*Dependency: {{Pkg|notify-sharp}}
+
* Makedependency: {{Pkg|mono-basic}}
*Makedependency: {{Pkg|mono-basic}}
+
* Build with: {{ic|vbnc -r:/usr/lib/mono/notify-sharp-3.0/notify-sharp.dll hello_world.vb}}
*Build with: {{ic|vbnc -r:/usr/lib/mono/notify-sharp/notify-sharp.dll hello_world.vb}}
+
* Run with: {{ic|mono hello_world.exe}}
*Run with: {{ic|mono hello_world.exe}}
+
  
{{hc|hello_world.vb|<nowiki>Imports Notifications
+
{{hc|hello_world.vb|2=
 +
Imports Notifications
 
Public Class Hello
 
Public Class Hello
 
Public Shared Sub Main
 
Public Shared Sub Main
Line 223: Line 487:
 
Hello.Show
 
Hello.Show
 
End Sub
 
End Sub
End Class</nowiki>}}
+
End Class
 +
}}
  
 
==See also==
 
==See also==

Latest revision as of 02:26, 7 December 2016

Related articles

Desktop notifications are small, passive popup dialogs that notify the user of particular events in an asynchronous manner.

Libnotify

Libnotify is an implementation of the Desktop Notifications Specification which provides support for GTK+ and Qt applications and is desktop independent: it is already used by many open source apps like Evolution and Pidgin. Libnotify can be installed with the libnotify package.

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

Notification servers

Built-in

The following desktop environments use their own implementations to display notifications, and you cannot replace them. Their notification servers are started automatically on login to receive notifications from applications via DBus.

  • Cinnamon provides a notification server itself. Notifications are displayed at the top right corner of the screen.
  • Enlightenment provides a notification server through its Notification extension. Notification options are configurable.
  • GNOME provides a notification server itself. Notifications are displayed at the top of the screen.
  • KDE Plasma provides a notification server itself. Notifications are displayed at the bottom right corner of the screen.

Standalone

In other desktop environments, the notification server needs to be launched using your WM's/DE's "autostart" option. (It can be launched on the first call via DBus, but that's not very desirable as it needs global configuration.)

You can choose one of the following implementations:

https://github.com/p12tic/awn-extras || awn-extras-appletsAUR
  • Deepin Notifications — Notification server for Deepin.
https://github.com/linuxdeepin/deepin-notifications || deepin-notifications
  • Dunst — Minimalistic notification daemon for Linux designed to fit nicely into minimalistic windowmanagers like dwm.
http://www.knopwob.org/dunst/ || dunst
  • LXQt Notification Daemon — Notification server for LXQt.
https://github.com/lxde/lxqt-notificationd || lxqt-notificationd
https://github.com/GNOME/notification-daemon || notification-daemon
You can run it manually using /usr/lib/notification-daemon-1.0/notification-daemon.
  • MATE Notification Daemon — Notification server for MATE.
https://github.com/mate-desktop/mate-notification-daemon/ || GTK+ 2: mate-notification-daemon, GTK+ 3 (experimental): mate-notification-daemon-gtk3[broken link: replaced by mate-notification-daemon]
  • Notify OSD — Notification server for Unity.
https://launchpad.net/notify-osd || notify-osd
  • statnot — 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.
https://github.com/halhen/statnot || statnotAUR
  • twmn — Notification system for tiling window managers.
https://github.com/sboli/twmn || twmn-gitAUR
  • Xfce Notification Daemon — Notification server for Xfce.
http://goodies.xfce.org/projects/applications/xfce4-notifyd || xfce4-notifyd
Tip: To configure xfce4-notifyd, run the following command: xfce4-notifyd-config.

Usage in programming

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
Tip:
  • An overview on the available icons can be found here.
  • To send desktop notification from a background script running as root (replace X_user and X_userid with the user and userid running X respectively):
    # sudo -u X_user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/X_userid/bus notify-send 'Hello world!' 'This is an example notification.'

Boo

  • Dependency: notify-sharp-3 (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>
int main() {
	notify_init ("Hello world!");
	NotifyNotification * Hello = notify_notification_new ("Hello world", "This is an example notification.", "dialog-information");
	notify_notification_show (Hello, NULL);
	g_object_unref(G_OBJECT(Hello));
	notify_uninit();
	return 0;
}

C++

  • Dependency: libnotifymmAUR
  • 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();
	return 0;
}

C#

  • Dependency: notify-sharp-3
  • Build with: mcs -pkg:notify-sharp-3.0 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();
	}
}

Cobra

  • Dependency: notify-sharp-3
  • Makedependency: cobraAUR
  • Build with: cobra -c hello_world
  • Run with: mono hello_world.exe
hello_world.cobra
@args -pkg:notify-sharp-3.0
use Notifications
class HelloWorld
    def main
        hello = Notification()
        hello.summary  = "Hello world!"
        hello.body     = "This is an example notification."
        hello.iconName = "dialog-information"
        hello.show

F#

  • Dependency: notify-sharp-3
  • Makedependency: fsharpAUR
  • Build with: fsharpc -r:notify-sharp.dll -I:/usr/lib/mono/notify-sharp-3.0/ -I:/usr/lib/mono/gtk-sharp-3.0/ hello_world.fs
  • Run with: mono hello_world.exe
hello_world.fs
open Notifications
let 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 Notify.Notification ("Hello world!","This is an example notification.","dialog-information")
	Hello.show ()

Go

  • Dependency: libnotify
  • Makedependency: go-notify-gitAUR
  • Build with: go build hello_world.go
  • (Or run with: go run hello_world.go)
hello_world.go
package main
import ("github.com/mqu/go-notify")

func main() {
	notify.Init("Hello world")
	hello := notify.NotificationNew("Hello World!", "This is an example notification.","dialog-information")
	hello.Show()
}

Groovy

  • Dependencies: groovy, java-gnomeAUR
  • Build with: groovyc -cp /usr/share/java/gtk.jar HelloWorld.groovy && jar cfe HelloWorld.jar HelloWorld HelloWorld.class
  • Run with: java -cp /usr/share/groovy/embeddable/groovy-all.jar:/usr/share/java/gtk.jar:HelloWorld.jar HelloWorld or groovy -cp /usr/share/java/gtk.jar HelloWorld.groovy
HelloWorld.groovy
import org.gnome.gtk.*
import org.gnome.notify.*

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

Haskell

hello_world.hs
import DBus.Notify
main = do
         client <- connectSession
         let hello = blankNote { summary="Hello world!",
                                 body=(Just $ Text "This is an example notification."),
                                 appImage=(Just $ Icon "dialog-information") }
         notification <- notify client hello
         return 0

IronPython

hello_world.py
import clr
clr.AddReference('notify-sharp')
import Notifications
Hello = Notifications.Notification()
Hello.Summary  = "Hello world!"
Hello.Body     = "This is an example notification."
Hello.IconName = "dialog-information"
Hello.Show()

Java

  • Dependency: java-gnomeAUR
  • Makedependency: java-environment
  • Build with: javac -cp /usr/share/java/gtk.jar HelloWorld.java && jar cfe HelloWorld.jar HelloWorld HelloWorld.class
  • Run with: java -cp /usr/share/java/gtk.jar:HelloWorld.jar 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
const Notify = imports.gi.Notify;
Notify.init ("Hello world");
var Hello=new Notify.Notification ({summary: "Hello world!",
                                    body: "This is an example notification.",
                                    "icon-name": "dialog-information"});
Hello.show ();

JRuby

  • Dependencies: java-gnomeAUR, jruby
  • Build with: jrubyc hello_world.rb && jar cfe hello_world.jar hello_world hello_world.class
  • Run with: java -cp /opt/jruby/lib/jruby.jar:hello_world.jar hello_world or jruby hello_world.rb
hello_world.rb
require '/usr/share/java/gtk.jar'
import Java::OrgGnomeGtk::Gtk
import Java::OrgGnomeNotify::Notify
import Java::OrgGnomeNotify::Notification

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

Jython

  • Dependencies: java-gnomeAUR, jython
  • Run with: jython -Dpython.path=/usr/share/java/gtk.jar hello_world.py
hello_world.py
from org.gnome.gtk import Gtk
from org.gnome.notify import Notify, Notification
Gtk.init(None)
Notify.init("Hello world")
Hello=Notification("Hello world!", "This is an example notification.", "dialog-information")
Hello.show()

Lua

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

Nemerle

  • Dependency: notify-sharp-3
  • Makedependency: nemerleAUR
  • Build with: ncc -pkg:notify-sharp-3.0 -out:hello_world.exe hello_world.n
  • Run with: mono hello_world.exe
hello_world.n
using Notifications;
public class HelloWorld {
	static Main() : void {
		def Hello = Notification();
		Hello.Summary  = "Hello world!";
		Hello.Body     = "This is an example notification.";
		Hello.IconName = "dialog-information";
		Hello.Show();
	}
}

Pascal

hello_world.pas
program	hello_world;
uses	libnotify;
var	hello : PNotifyNotification;
begin
	notify_init(argv[0]);
	hello := notify_notification_new ('Hello world', 'This is an example notification.', 'dialog-information');
	notify_notification_show (hello, nil);
end.

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;


Python

hello_world.py
#!/usr/bin/python
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
Notify.init("Hello world")
Hello=Notify.Notification.new("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

Rust

Using notify-rust.

  • Makedependency: cargo
  • Build with: cargo build
  • Run with: target/debug/hello_world or cargo run
Cargo.toml
[package]
name = "hello_world"
version = "0.1.0"

[dependencies]
notify-rust = "^3"
src/main.rs
extern crate notify_rust;
use notify_rust::Notification;
fn main(){
    Notification::new()
        .summary("Hello world")
        .body("This is an example notification.")
        .icon("dialog-information")
        .show().unwrap();
}

Scala

  • Dependency: java-gnomeAUR (and scala)
  • Makedependency: scala
  • Build with: scalac -cp /usr/share/java/gtk.jar -d HelloWorld.jar HelloWorld.scala
  • Run with: java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld (or scala -cp /usr/share/java/gtk.jar HelloWorld.scala)
HelloWorld.scala
import org.gnome.gtk._
import org.gnome.notify._

object HelloWorld {
  def main(args: Array[String]) {
    Gtk.init(args)
    Notify.init("Hello world")
    var Hello = new Notification("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 Notify.Notification("Hello world!", "This is an example notification.", "dialog-information");
		Hello.show ();
	}
}

Visual Basic .NET

  • Dependency: notify-sharp-3
  • Makedependency: mono-basic
  • Build with: vbnc -r:/usr/lib/mono/notify-sharp-3.0/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