Desktop notifications

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 applications 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

Cinnamon, Deepin, Enlightenment, GNOME, GNOME Flashback and KDE Plasma use their own implementations to display notifications, and it cannot be replaced. Their notification servers are started automatically on login to receive notifications from applications via DBus.

Standalone

In other desktop environments, the notification server needs to be launched using an autostarting option.

Alternatively, making the notification server as a D-Bus service, the notification server can be launched automatically on the first call to it. For example, after installing the notification-daemon package, add the following configuration to D-Bus services directory (/usr/share/dbus-1/services or $XDG_DATA_HOME/dbus-1/services):

org.freedesktop.Notifications.service
[D-BUS Service]
Name=org.freedesktop.Notifications
Exec=/usr/lib/notification-daemon-1.0/notification-daemon

Whenever an application sends a notification by sending a signal to org.freedesktop.Notifications, D-Bus activates /usr/lib/notification-daemon-1.0/notification-daemon if it has not already been activated.

You can also choose one of the following implementations:

  • mako A lightweight notification daemon for Wayland; it currently works on Sway.
https://github.com/emersion/mako || mako
  • MATE Notification Daemon Notification server for MATE.
https://github.com/mate-desktop/mate-notification-daemon/ || mate-notification-daemon
    You can run it manually using /usr/lib/notification-daemon-1.0/notification-daemon.
    You can run it manually using .
    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 a simple "Hello world" notification.

    Bash

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

    Boo

    • Dependency: ()
    • Makedependency:
    • Build with:
    • Run with: (or )
    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:
    • Build with:
    hello_world.c
    #include <gio/gio.h>
    int main() {
    	GApplication *application = g_application_new ("hello.world", G_APPLICATION_FLAGS_NONE);
    	g_application_register (application, NULL, NULL);
    	GNotification *notification = g_notification_new ("Hello world!");
    	g_notification_set_body (notification, "This is an example notification.");
    	GIcon *icon = g_themed_icon_new ("dialog-information");
    	g_notification_set_icon (notification, icon);
    	g_application_send_notification (application, NULL, notification);
    	g_object_unref (icon);
    	g_object_unref (notification);
    	g_object_unref (application);
    	return 0;
    }
    • Dependency:
    • Build with:

    C++

    • Dependency:
    • Build with:
    hello_world.cc
    #include <giomm-2.4/giomm.h>
    int main(int argc, char *argv[]) {
    	auto Application = Gio::Application::create("hello.world", Gio::APPLICATION_FLAGS_NONE);
    	Application->register_application();
    	auto Notification = Gio::Notification::create("Hello world");
    	Notification->set_body("This is an example notification.");
    	auto Icon = Gio::ThemedIcon::create("dialog-information");
    	Notification->set_icon (Icon);
    	Application->send_notification(Notification);
    	return 0;
    }
    • Dependency:
    • Build with:

    C#

    • Dependency:
    • Build with: mcs -pkg:notify-sharp-3.0 hello_world.cs
    • Run with:

    Crystal

    • Dependency: woodruffw/notify.cr (from shards)
    • Build with: shards build

    F#

    • Dependency:
    • Makedependency:
    • Build with:
    • Run with:

    Genie

    • Dependency:
    • Makedependency: vala
    • Build with:
    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:
    • Build with:
    • (Or run with: )

    Groovy

    • Dependencies: , java-gnomeAUR
    • Build with:
    • Run with: or
    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

    • Makedependency:
    • Build with:

    IronPython

    • Dependencies: ,
    • Run with:
    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:
    • Run with:

    JavaScript

    • Dependency: gjs

    JRuby

    • Dependencies: java-gnomeAUR,
    • Build with:
    • Run with: java -cp /opt/jruby/lib/jruby.jar:hello_world.jar hello_world or

    Jython

    Lua

    Nemerle

    • Dependency:
    • Makedependency:
    • Build with:
    • Run with:

    Pascal

    Using libnotify

    Using direct D-Bus calls

    • Dependencies:
    hello_world.pl
    #!/usr/bin/perl
    use Net::DBus;
    my $bus = Net::DBus->session;
    my $svc = $bus->get_service('org.freedesktop.Notifications');
    my $obj = $svc->get_object('/org/freedesktop/Notifications');
    my $id = $obj->Notify('myapp', 0,
                          'dialog-information',
                          'Hello world!',
                          'This is an example notification.',
                          [], {}, 0);

    Python

    • Dependency:
    • Dependency:
    hello_world.py
    #!/usr/bin/env python3
    import dbus
    
    obj = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
    obj = dbus.Interface(obj, "org.freedesktop.Notifications")
    obj.Notify("", 0, "", "Hello world", "This is an example notification.", [], {"urgency": 1}, 10000)

    For the arguments in the method , please refer to the section of org.freedesktop.Notifications.Notify at Desktop Notifications Specification.

    Ruby

    Rust

    Using notify-rust.

    • Makedependency:
    • Build with:
    • Run with: target/debug/hello_world or

    Scala

    Vala

    • Dependency:
    • Makedependency: vala
    • Build with:
    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:
    • Makedependency:
    • Build with:
    • Run with:

    Tips and tricks

    Replace previous notification

    Notifications can be replaced if their ID is known; if a new notification request specifies the same ID, it will always replace the old notification. (The libnotify bindings shown above handle this automatically.) Unfortunately notify-send does not report this ID, so alternative tools are required to do this on CLI. One capable CLI-tool is the notify-send.py python script, which provides notify-send syntax with additional ID-reporting and replacing capabilities.

    However, with some notification servers (such as Notify-OSD), you can use the hint with notify-send to achieve the same result.

    For example, to get a notification displaying time:

    Include Buttons or listen for close/on-click of the notification

    With the notify-send.py script, actions can be used to display buttons or to listen for the default-action of the notification (usually, when the user clicks on it) and the close-action. When the action-icons hint is set to true and the notification daemon supports this, the buttons will display icons instead of text. The script prints the action identifier or "close" to the command line when the corresponding event has occured. To listen for the default action (on-click), one has to use the action-identfier "default".

    Example with icons on buttons:

    notify-send.py "Buttons" "Do you like em?" --hint boolean:action-icons:true --action yes:face-cool no:face-sick

    Multiple notification servers with D-Bus services

    As described in the section Standalone, users can create a D-Bus service so that a notification server can be launched automatically. Some implementations already include the D-Bus service files. However, this causes a problem when multiple notification servers are installed and when some of them come with the service files. For example, installing both and without explicitly specifying the desired server, D-Bus then chooses one for the users, and the decision is out of users' control. To avoid the situation, you can override the service used by creating the symbolic link pointing to the service you want to use, and then restart the session.

    Troubleshooting

    Applications hanging for exactly one minute

    If applications hang when attempting to show notifications, it might be because of a notification service falsely advertising its availability through the D-Bus service.

    For instance, suppose a user recently installed a KDE component that requires , but the user is still running XFCE. In this case, the KDE notifier will be prioritized, but the user is not running it. The application will hang while waiting for the service, and only after a timeout will it fall back to .

    The most noticeable hanging might come from the volume indicator scroll adjustment.

    If you are in this situation, you should have two notification handlers:

    Of those two, one fails regularly after a 1-minute timeout, as seen in the journal:

    # journalctl -g notif
    [ press End to go to the end of the log ]
    Jul 01 09:40:49 laptop dbus-daemon[866]: [session uid=1000 pid=866] Activating service name='org.freedesktop.Notifications' requested by ':1.193' (uid=1000 pid=5432 comm="/usr/lib/xfce4/panel/wrapper-2.0 /usr/lib/xfce4/pa")
    Jul 01 09:41:49 laptop plasma_waitforname[6093]: org.kde.knotifications: WaitForName: Service was not registered within timeout
    Jul 01 09:41:49 laptop dbus-daemon[866]: [session uid=1000 pid=866] Activated service 'org.freedesktop.Notifications' failed: Process org.freedesktop.Notifications exited with status 1

    Choosing the service you want to use as described in #Multiple notification servers with D-Bus services will fix the problem.

    See also

    This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.