Desktop notifications (日本語)
関連記事
デスクトップ通知は、非同期に特定のイベントをユーザーに通知する小さくて控えめなポップアップダイアログです。
Libnotify
Libnotify は GTK+ や Qt アプリケーションのサポートを提供する Desktop Notifications Specification の実装で特定のデスクトップに依存していません: Evolution や Pidgin など多数のオープンソースアプリによって使用されています。Libnotify は公式リポジトリにある libnotify パッケージでインストールすることが可能です。
libnotify を使うには、通知サーバーをインストールする必要があります。
通知サーバー
ビルトイン
以下のデスクトップ環境では通知を表示するためにそれぞれ独自の実装を使っており、置き換えることができません。通知サーバーはログイン時に自動で起動し DBus によってアプリケーションからの通知を受け取ります。
- Avant Window Navigator には awn-extras-applets による通知デーモンアプレットがあります。
- Cinnamon は通知サーバーを備えており、通知は画面の右上に表示されます。
- Enlightenment は Notification モジュールを通して通知サーバーを提供しています。通知は画面の右上に表示されます。
- GNOME は通知サーバーを備えており、通知は画面の下に表示されます。
- KDE は kdebase-runtime パッケージに入っている knotify4 を使って通知を表示します。通知は画面の右下に表示されます。
スタンドアロン
他のデスクトップ環境では、DBus で初めて呼ばれた時に通知サーバーが起動します。以下の実装からどれか一つを選ぶことが可能です:
- dunst は dwm などの最小主義のウィンドウマネージャによく似合うように作られた最小主義の通知デーモンです。
- notification-daemon は GNOME Flashback が使っている通知デーモンです。デフォルトでは D-Bus サービスファイルを持っていません。GNOME Flashback 以外から使うには、以下のファイルを作成してください:
/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
- mate-notification-daemon は MATE の通知サーバーで、公式リポジトリからインストールできます。
- notify-osd は Unity の通知サーバーで、公式リポジトリからインストールできます。
- statnot は小さくて軽量な通知デーモンで、ルートウィンドウのタイトルや標準出力、FIFO パイプに通知を出力することができ、タイル型ウィンドウマネージャと上手く統合されます。Arch User Repository または git リポジトリから利用できます。
- twmn-git はタイル型ウィンドウマネージャのための通知システムです。Arch User Repository または git リポジトリから利用できます。
- xfce4-notifyd は Xfce の通知サーバーで、公式リポジトリからインストールできます。
プログラミングでの使い方
GObject-Introspection やバインディングを通して多くのプログラミング言語を使ったり、または bash を利用して簡単に libnotify でメッセージを表示することができます。
以下の例ではシンプルな "Hello world" の通知が表示されます。
Bash
- 依存パッケージ: libnotify
hello_world.sh
#!/bin/bash notify-send 'Hello world!' 'This is an example notification.' --icon=dialog-information
Boo
- 依存パッケージ: notify-sharp-3 (boo)
- ビルドするのに必要なパッケージ: boo
- ビルド:
booc hello_world.boo - 実行:
mono hello_world.exe(または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
- 依存パッケージ: libnotify
- ビルド:
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);
g_object_unref(G_OBJECT(Hello));
notify_uninit();
}
C++
- 依存パッケージ: AUR の libnotifymm
- ビルド:
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#
- 依存パッケージ: notify-sharp-3
- ビルド:
mcs -pkg:notify-sharp-3.0 hello_world.cs - 実行:
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
- 依存パッケージ: notify-sharp-3
- ビルドするのに必要なパッケージ: AUR の cobra
- ビルド:
cobra -c hello_world - 実行:
mono hello_world.exe
hello_world.cs
@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#
- 依存パッケージ: notify-sharp-3
- ビルドするのに必要なパッケージ: AUR の fsharp
- ビルド:
fsharpc -r:notify-sharp.dll -I:/usr/lib/mono/notify-sharp-3.0/ -I:/usr/lib/mono/gtk-sharp-3.0/ hello_world.fs - 実行:
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
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 ()
Groovy
- 依存パッケージ: groovy, AUR の java-gnome
- ビルド:
groovyc -cp /usr/share/java/gtk.jar HelloWorld.groovy && jar cfe HelloWorld.jar HelloWorld HelloWorld.class - 実行:
java -cp /usr/share/groovy/embeddable/groovy-all.jar:/usr/share/java/gtk.jar:HelloWorld.jar HelloWorld(または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()
Java
- 依存パッケージ: AUR の java-gnome
- ビルドするのに必要なパッケージ: java-environment
- ビルド:
javac -cp /usr/share/java/gtk.jar HelloWorld.java && jar cfe HelloWorld.jar HelloWorld HelloWorld.class - 実行:
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
#!/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 ();
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()</nowiki>
Perl
- 依存パッケージ: libnotify, AUR の perl-glib-object-introspection
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
- 依存パッケージ: libnotify, python-gobject (または Python 2 なら python2-gobject)
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 ()
Ruby
- 依存パッケージ: libnotify, AUR の ruby-gir_ffi
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
Scala
- 依存パッケージ: AUR の java-gnome (と scala)
- ビルドするのに必要なパッケージ: scala
- ビルド:
scalac -cp /usr/share/java/gtk.jar -d HelloWorld.jar HelloWorld.scala - 実行:
java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld(または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
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
- 依存パッケージ: notify-sharp-3
- ビルドするのに必要なパッケージ: mono-basic
- ビルド:
vbnc -r:/usr/lib/mono/notify-sharp-3.0/notify-sharp.dll hello_world.vb - 実行:
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
参照
- Libnotify リファレンスマニュアル
- C サンプル
- Python サンプル (フランス語の記事)