「デスクトップ通知」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(1版 をインポートしました)
(TranslationStatus の時刻形式を合わせる)
 
(8人の利用者による、間の31版が非表示)
1行目: 1行目:
 
[[Category:X サーバー]]
 
[[Category:X サーバー]]
[[Category:開発]]
+
[[Category:通知]]
  +
[[Category:ソフトウェア一覧]]
 
[[en:Desktop notifications]]
 
[[en:Desktop notifications]]
  +
[[zh-hans:桌面通知程序]]
[[es:Desktop notifications]]
 
[[ru:Desktop notifications]]
 
 
{{Related articles start}}
 
{{Related articles start}}
{{Related2|GTK+|GTK+}}
+
{{Related|GTK}}
 
{{Related|Libcanberra}}
 
{{Related|Libcanberra}}
 
{{Related articles end}}
 
{{Related articles end}}
 
デスクトップ通知は、非同期に特定のイベントをユーザーに通知する小さくて控えめなポップアップダイアログです。
 
デスクトップ通知は、非同期に特定のイベントをユーザーに通知する小さくて控えめなポップアップダイアログです。
   
==Libnotify==
+
== Libnotify ==
   
Libnotify は [[GTK+|GTK+]] [[Qt|Qt]] アプリケーションのサポートを提供する [http://developer.gnome.org/notification-spec/ Desktop Notifications Specification] の実装で特定のデスクトップに依存していません: [[Evolution]] や [[Pidgin|Pidgin]] などのオープンソースアプリによって使用されています。Libnotify は[[公式リポジトリ]]にある {{Pkg|libnotify}} パッケージでインストールすることが可能です。
+
{{Pkg|libnotify}} {{man|1|notify-send}} ユーティリティや [[GTK]] [[Qt]] アプリケーションのサポートを提供する [https://specifications.freedesktop.org/notification-spec/latest/ Desktop Notifications Specification] の実装で特定のデスクトップに依存していません。この実装はすでに [[Evolution]] や [[Pidgin]] といったのオープンソースアプリによって使用されています。
   
  +
{{Tip|利用可能なアイコンに関する概要は[https://specifications.freedesktop.org/icon-naming-spec/latest/ 仕様]にあります。}}
libnotify を使うには、[[#通知サーバー|通知サーバー]]をインストールする必要があります。
 
   
 
== 通知サーバー ==
 
== 通知サーバー ==
   
  +
[[#Libnotify|libnotify]] で送られた通知を受け取るには、通知サーバーが必要です。
===ビルトイン===
 
以下のデスクトップ環境では通知を表示するためにそれぞれ独自の実装を使っており、置き換えることができません。通知サーバーはログイン時に自動で起動し DBus によってアプリケーションからの通知を受け取ります。
 
   
  +
=== ビルトイン ===
* [[Avant Window Navigator]] には {{AUR|awn-extras-applets}} による通知デーモンアプレットがあります。
 
* [[Cinnamon|Cinnamon]] は通知サーバーを備えており、通知は画面の右上に表示されます。
 
* [[Enlightenment|Enlightenment]] は Notification モジュールを通して通知サーバーを提供しています。通知は画面の右上に表示されます。
 
* [[GNOME|GNOME]] は通知サーバーを備えており、通知は画面の下に表示されます。
 
* [[KDE|KDE]] は {{Pkg|kdebase-runtime}} パッケージに入っている '''knotify4''' を使って通知を表示します。通知は画面の右下に表示されます。
 
   
  +
[[Cinnamon]]、[[Deepin]]、[[Enlightenment]]、[[GNOME]]、[[GNOME Flashback]] は独自の実装を用いて通知を表示します。これらの通知サーバーはアプリケーションからの通知を DBus 経由で受け取るためにログイン時に自動的に起動されるので、他の通知サーバーに置き換えることができない場合があります。
===スタンドアロン===
 
他のデスクトップ環境では、DBus で初めて呼ばれた時に通知サーバーが起動します。以下の実装からどれか一つを選ぶことが可能です:
 
* {{pkg|dunst}} は [[dwm]] などの最小主義のウィンドウマネージャによく似合うように作られた最小主義の通知デーモンです。
 
* {{pkg|notification-daemon}} は [[GNOME Flashback|GNOME Flashback]] が使っている通知デーモンです。デフォルトでは D-Bus サービスファイルを持っていません。GNOME Flashback 以外から使うには、以下のファイルを作成してください:
 
{{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|mate-notification-daemon}} は [[MATE|MATE]] の通知サーバーで、公式リポジトリからインストールできます。
 
* {{Pkg|notify-osd}} は [[Unity|Unity]] の通知サーバーで、公式リポジトリからインストールできます。
 
* {{AUR|statnot}} は小さくて軽量な通知デーモンで、ルートウィンドウのタイトルや標準出力、FIFO パイプに通知を出力することができ、タイル型ウィンドウマネージャと上手く統合されます。[[Arch User Repository|Arch User Repository]] または [https://github.com/halhen/statnot git リポジトリ]から利用できます。
 
* {{AUR|twmn-git}} はタイル型ウィンドウマネージャのための通知システムです。[[Arch User Repository|Arch User Repository]] または [https://github.com/sboli/twmn git リポジトリ]から利用できます。
 
* {{Pkg|xfce4-notifyd}} は [[Xfce|Xfce]] の通知サーバーで、公式リポジトリからインストールできます。
 
:{{Tip|xfce4-notifyd を設定するには、ターミナルから次のコマンドを実行してください: {{ic|xfce4-notifyd-config}}。}}
 
   
  +
[[KDE Plasma]] では、システムトレイの設定を開き、システムサービスの項目から Notifications の横にあるドロップダウンを Disabled に変更することで、ビルトインの通知サーバーを無効化することができます。その後システム設定メニューで System / Autostart のところに新しい自動起動アプリケーションを追加することで、所望の通知サーバーを追加することができます。変更を適用するには一旦ログアウトし、ログインし直す必要があります。
==プログラミングでの使い方==
 
GObject-Introspection やバインディングを通して多くのプログラミング言語を使ったり、または bash を利用して簡単に libnotify でメッセージを表示することができます。
 
   
  +
=== スタンドアロン ===
以下の例ではシンプルな "Hello world" の通知が表示されます。
 
   
  +
他のデスクトップ環境では、通知サーバーは手動でインストールし、[[XDG Autostart]] などを使って起動する必要があります。
'''Bash'''
 
   
  +
あるいは、通知サーバーを [[D-Bus]] サービスにすることで、通知サーバーを初めて呼び出した時に自動的に起動させることもできます。ほとんどの通知サーバーにはすでに dbus サービスが {{ic|/usr/share/dbus-1/services}} に用意されています。{{Pkg|notification-daemon}} などの一部の実装では、ユーザー [[D-Bus]] サービスのディレクトリ ({{ic|$XDG_DATA_HOME/dbus-1/services}}) 内に手動でサービスを作る必要があります:
* 依存パッケージ: {{Pkg|libnotify}}
 
   
  +
{{hc|org.freedesktop.Notifications.service|2=
{{hc|hello_world.sh|2=
 
  +
[D-BUS Service]
#!/bin/bash
 
  +
Name=org.freedesktop.Notifications
notify-send 'Hello world!' 'This is an example notification.' --icon=dialog-information
 
  +
Exec=/usr/lib/notification-daemon-1.0/notification-daemon
 
}}
 
}}
   
  +
アプリケーションが通知を送ると (つまり {{ic|org.freedesktop.Notifications}} にシグナルを送ると)、{{ic|/usr/lib/notification-daemon-1.0/notification-daemon}} がまだアクティブ化されていない場合、D-Bus によってアクティブ化されます。
{{Tip|利用可能なアイコンは[http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html ここ]から参照できます。}}
 
   
  +
通知サーバーは以下から選ぶことができます:
'''Boo'''
 
   
  +
* {{App|Deadd Notification Center|Dunst にインスパイヤされた、通知センターが付いた通知デーモン。[https://github.com/phuhl/linux_notification_center/issues/255 2025年2月から開発が滞っています。]|https://github.com/phuhl/linux_notification_center|{{AUR|deadd-notification-center}}}}
* 依存パッケージ: {{Pkg|notify-sharp-3}} ({{Pkg|boo}})
 
  +
* {{App|[[Dunst]]|[[dwm]] などのミニマルなウィンドウマネージャにうってつけの Linux 用の最小通知デーモン。1.6 から Wayland サポートがあります。|https://dunst-project.org/|{{Pkg|dunst}}}}
* ビルドするのに必要なパッケージ: {{Pkg|boo}}
 
  +
* {{App|fnott|wlroots ベースのコンポジタのための軽量な Wayland 向けの通知デーモン。キーボードによって操作可能。|https://codeberg.org/dnkl/fnott|{{Pkg|fnott}}}}
* ビルド: {{ic|booc hello_world.boo}}
 
  +
* {{App|LXQt Notification Daemon|[[LXQt]] の通知サーバー。|https://github.com/lxde/lxqt-notificationd|{{Pkg|lxqt-notificationd}}}}
* 実行: {{ic|mono hello_world.exe}} (または {{ic|booi hello_world.boo}})
 
  +
* {{App|mako|[[Wayland]] 向けの軽量な通知サーバー。現時点では [[Sway]] と {{Pkg|river}} 上で動作します。|https://github.com/emersion/mako|{{Pkg|mako}}}}
  +
* {{App|MATE Notification Daemon|[[MATE]] の通知サーバー。|https://github.com/mate-desktop/mate-notification-daemon/|{{Pkg|mate-notification-daemon}}}}
  +
* {{App|Notification Daemon|オリジナルの通知サーバー。|https://gitlab.gnome.org/Archive/notification-daemon|{{Pkg|notification-daemon}}}}
  +
:{{ic|/usr/lib/notification-daemon-1.0/notification-daemon}} で手動で起動できます。
  +
* {{App|Notify OSD|Unity の通知サーバー。|https://launchpad.net/notify-osd|{{Pkg|notify-osd}}}}
  +
* {{App|statnot|小さくて軽量な通知デーモン。ルートウィンドウのタイトルや標準出力、FIFO パイプなどに通知を出力できるので、タイル型ウィンドウマネージャと相性がとても良いです。|https://github.com/halhen/statnot|{{AUR|statnot}}}}
  +
* {{App|swaync|[[Sway]] のためのシンプルな GTK ベースの通知デーモン。|https://github.com/ErikReider/SwayNotificationCenter|{{Pkg|swaync}}}}
  +
* {{App|twmn|タイル型ウィンドウマネージャ向けの通知システム。|https://github.com/sboli/twmn|{{AUR|twmn-git}}}}
  +
* {{App|wired|レイヤーブロックのカスタマイズ性が高い軽量な通知デーモン。Rust で書かれている。|https://github.com/Toqozz/wired-notify|{{AUR|wired}}}}
  +
* {{App|Xfce Notification Daemon|[[Xfce]] の通知サーバー。|https://docs.xfce.org/apps/notifyd/start|{{Pkg|xfce4-notifyd}}}}
  +
:{{ic|/usr/lib/xfce4/notifyd/xfce4-notifyd}} で手動で起動できます。
  +
:{{Tip|xfce4-notifyd を設定するには、ターミナルから次のコマンドを実行してください: {{ic|xfce4-notifyd-config}}。}}
   
  +
== ヒントとテクニック ==
{{hc|hello_world.boo|2=
 
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'''
 
   
  +
{{man|1|systemd-run}} を使うことで (例えば root として実行されたバックグラウンドのスクリプトなどから)、他のユーザーのセッションに入り、そのユーザーに通知を送ることができます:
* 依存パッケージ: {{Pkg|libnotify}}
 
* ビルド: {{ic|gcc -o hello_world `pkg-config --cflags --libs libnotify` hello_world.c}}
 
   
  +
# systemd-run --machine=''target_user''@.host --user notify-send 'Hello world!' 'This is an example notification.'
{{hc|hello_world.c|2=
 
#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();
 
}
 
}}
 
   
  +
もう一つの方法は {{Pkg|systembus-notify}} です。以下のコマンドは、{{ic|systembus-notify}} をセッション内に実行しているすべてのユーザーに通知を表示します:
'''C++'''
 
   
  +
$ dbus-send --system / net.nuetzlich.SystemNotifications.Notify 'string:Hello world!' 'string:This is an example notification.'
* 依存パッケージ: AUR の {{AUR|libnotifymm}}
 
* ビルド: {{Ic|g++ -o hello_world `pkg-config --cflags --libs libnotifymm-1.0` hello_world.cc}}
 
   
  +
{{Tip|[[systemd#サービスの失敗を通知する]] で説明されているようにして、上記のコマンドを {{ic|ExecStart}} コマンドで実行することで、失敗したサービスを通知で知らせることもできます。}}
{{hc|hello_world.cc|2=
 
#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#'''
 
   
  +
通知 ID がわかっている場合は、その通知を置き換えることができます。新しい通知リクエストで同じ ID が指定されている場合、常に古い通知が置き換えられます。(上記の libnotify バインディングはこれを自動的に処理します。) 残念ながら、notify-send はこの ID を報告しないため、CLI でこれを行うには代替ツールが必要です。有効な CLI ツールの 1 つは、[https://github.com/phuhl/notify-send.py notify-send.py] Python スクリプトです。これは、追加の ID レポート機能と置換機能を備えた Notify-Send 構文を提供します。
* 依存パッケージ: {{Pkg|notify-sharp-3}}
 
* ビルド: {{ic|mcs -pkg:notify-sharp-3.0 hello_world.cs}}
 
* 実行: {{ic|mono hello_world.exe}}
 
   
  +
ただし、''一部の'' 通知サーバー (Notify-OSD など) では、notify-send で {{ic|string:x-canonical-private-synchronous:}} ヒントを使用して同じ結果を得ることができます。
{{hc|hello_world.cs|2=
 
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'''
 
  +
{{bc|1=
 
  +
while true; do
* 依存パッケージ: {{Pkg|notify-sharp-3}}
 
  +
date=$(date)
* ビルドするのに必要なパッケージ: AUR の {{AUR|cobra}}
 
  +
notify-send "$date" -h string:x-canonical-private-synchronous:my-notification
* ビルド: {{ic|cobra -c hello_world}}
 
  +
sleep 1
* 実行: {{ic|mono hello_world.exe}}
 
  +
done
 
{{hc|hello_world.cs|<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#'''
 
 
* 依存パッケージ: {{Pkg|notify-sharp-3}}
 
* ビルドするのに必要なパッケージ: AUR の {{AUR|fsharp}}
 
* ビルド: {{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}}
 
* 実行: {{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'''
 
 
* 依存パッケージ: {{Pkg|libnotify}}
 
* ビルドするのに必要なパッケージ: {{Pkg|vala}}
 
* ビルド: {{ic|valac --pkg libnotify hello_world.gs}}
 
   
  +
[https://github.com/phuhl/notify-send.py notify-send.py] スクリプトを使用すると、アクションを使用してボタンを表示したり通知のデフォルトアクション (通常、ユーザーが通知をクリックした時に発生します) やクローズアクションをリッスンすることができます。''action-icons'' ヒントが true に設定されており、通知デーモンがこれをサポートしている場合、ボタンにはテキストの代わりにアイコンが表示されます。スクリプトは、対応するイベントが発生したときに、コマンドラインにアクション ID または "close" を出力します。デフォルトのアクション (クリック時) をリッスンするには、アクション識別子 "default" を使用する必要があります。
{{hc|hello_world.gs|2=
 
uses
 
Notify
 
   
  +
ボタンにアイコンを表示する例:
init
 
  +
{{bc|1=
Notify.init ("Hello world")
 
  +
notify-send.py "Buttons" "Do you like em?" --hint boolean:action-icons:true --action yes:face-cool no:face-sick
var Hello=new Notification ("Hello world!","This is an example notification.","dialog-information")
 
Hello.show ()
 
 
}}
 
}}
   
  +
=== D-Bus サービスが同梱されている通知サーバーが複数ある場合 ===
'''Groovy'''
 
   
  +
[[#スタンドアロン|スタンドアロン]]章で説明した通り、ユーザーは D-Bus サービスを作成して通知サーバーを自動的に起動させることができます。しかし、一部の通知サーバーにはすでに D-Bus サービスファイルが同梱されています。複数の通知サーバーをインストールしていて、それらのうちどれかにサービスファイルが含まれている場合、問題が発生します。例えば、{{Pkg|dunst}} と {{Pkg|mako}} の両方をインストールして、使用するサーバーを明示的に指定しなかった場合、D-Bus はどちらか一方を勝手に選びます。そうならないようにするには、{{ic|org.freedesktop.Notifications.service}} ([[#スタンドアロン]] を参照) を作成して、使用したいサービスを指定することで、使用されるサービスをオーバーライドすることができます。ファイルの作成後は、セッションを再起動してください。
* 依存パッケージ: {{Pkg|groovy}}, AUR の {{AUR|java-gnome}}
 
* ビルド: {{ic|groovyc -cp /usr/share/java/gtk.jar HelloWorld.groovy && jar cfe HelloWorld.jar HelloWorld HelloWorld.class}}
 
* 実行: {{ic|java -cp /usr/share/groovy/embeddable/groovy-all.jar:/usr/share/java/gtk.jar:HelloWorld.jar HelloWorld}} (または {{ic|groovy -cp /usr/share/java/gtk.jar HelloWorld.groovy}})
 
   
  +
== トラブルシューティング ==
{{hc|HelloWorld.groovy|2=
 
import org.gnome.gtk.*
 
import org.gnome.notify.*
 
   
  +
=== アプリケーションがちょうど 1 分間ハングする ===
Gtk.init()
 
Notify.init("Hello world")
 
def Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information")
 
Hello.show()
 
}}
 
   
  +
アプリケーションが通知を表示しようとした際にハングしてしまう場合、通知サービスが自身の可用性を誤って D-Bus サービス経由で広告してしまっているのかもしれません。
'''Java'''
 
   
  +
たとえば、ユーザーが最近、{{Pkg|plasma-workspace}} を必要とする KDE コンポーネントをインストールしたが、まだ XFCE を実行しているとします。この場合、KDE ​​通知機能が優先されますが、ユーザーはそれを実行していません。アプリケーションはサービスの待機中にハングし、タイムアウト後に {{Pkg|xfce4-notifyd}} にフォールバックすることになります。
* 依存パッケージ: AUR の {{AUR|java-gnome}}
 
* ビルドするのに必要なパッケージ: java-environment
 
* ビルド: {{ic|javac -cp /usr/share/java/gtk.jar HelloWorld.java && jar cfe HelloWorld.jar HelloWorld HelloWorld.class}}
 
* 実行: {{ic|java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld}}
 
   
  +
最もわかりやすいハングは、通知インディケータの調整スクロールでしょう。
{{hc|HelloWorld.java|2=
 
import org.gnome.gtk.Gtk;
 
import org.gnome.notify.Notify;
 
import org.gnome.notify.Notification;
 
   
  +
このような場合、2つの通知ハンドラーが存在するはずです:
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'''
 
 
* 依存パッケージ: {{Pkg|libnotify}}, {{Pkg|gjs}} ({{Pkg|seed}} でも動きます)
 
   
  +
{{hc|$ find /usr/share/dbus-1/services/ -name '*Notif*'|
{{hc|hello_world.js|2=
 
  +
org.kde.plasma.Notifications.service
#!/usr/bin/gjs
 
  +
org.xfce.xfce4-notifyd.Notifications.service
#!/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 ();
 
 
}}
 
}}
   
  +
[[journal]] を見ると、これら2つのうちどちら一方が定期的に1分のタイムアウトで失敗しています:
'''Lua'''
 
   
  +
{{hc|# journalctl -g notif|2=
* 依存パッケージ: {{Pkg|libnotify}}, {{Pkg|lua-lgi}}
 
  +
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
{{hc|hello_world.lua|2=
 
  +
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
#!/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'''
 
 
* 依存パッケージ: {{Pkg|libnotify}}, AUR の {{AUR|perl-glib-object-introspection}}
 
{{hc|hello_world.pl|2=
 
#!/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;
 
 
}}
 
}}
   
  +
[[#D-Bus サービスが同梱されている通知サーバーが複数ある場合]] で説明されているように、使いたいサービスを指定すれば、問題は解決します。
'''Python'''
 
   
  +
== 参照 ==
* 依存パッケージ: {{Pkg|libnotify}}, {{Pkg|python-gobject}} (または Python 2 なら {{Pkg|python2-gobject}})
 
   
  +
* [https://gnome.pages.gitlab.gnome.org/libnotify/ Libnotify リファレンスマニュアル]
{{hc|hello_world.py|2=
 
  +
* [https://web.archive.org/web/20160313162757/http://milky.manishsinha.net/2009/03/29/working-with-libnotify/ C サンプル] (アーカイブされたバージョン)
#!/usr/bin/python
 
  +
* [https://www.devdungeon.com/content/desktop-notifications-linux-python Python の通知サンプル]
from gi.repository import Notify
 
  +
* [https://hashbang.fr/tutoriel-notify.html Python の通知サンプル] (フランス語の記事)
Notify.init ("Hello world")
 
Hello=Notify.Notification.new ("Hello world","This is an example notification.","dialog-information")
 
Hello.show ()
 
}}
 
 
'''Ruby'''
 
 
*依存パッケージ: {{Pkg|libnotify}}, AUR の {{AUR|ruby-gir_ffi}}
 
 
{{hc|hello_world.rb|2=
 
#!/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 の {{AUR|java-gnome}} (と {{Pkg|scala}})
 
* ビルドするのに必要なパッケージ: {{Pkg|scala}}
 
* ビルド: {{ic|scalac -cp /usr/share/java/gtk.jar -d HelloWorld.jar HelloWorld.scala}}
 
* 実行: {{ic|java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld}} (または {{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'''
 
 
*依存パッケージ: {{Pkg|libnotify}}
 
*ビルドするのに必要なパッケージ: {{Pkg|vala}}
 
*ビルド: {{ic|valac --pkg libnotify hello_world.vala}}
 
 
{{hc|hello_world.vala|2=
 
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'''
 
 
* 依存パッケージ: {{Pkg|notify-sharp-3}}
 
* ビルドするのに必要なパッケージ: {{Pkg|mono-basic}}
 
* ビルド: {{ic|vbnc -r:/usr/lib/mono/notify-sharp-3.0/notify-sharp.dll hello_world.vb}}
 
* 実行: {{ic|mono hello_world.exe}}
 
 
{{hc|hello_world.vb|2=
 
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
 
}}
 
   
  +
{{TranslationStatus|Desktop notifications|2025-07-03|839792}}
==参照==
 
*[http://developer.gnome.org/libnotify/ Libnotify リファレンスマニュアル]
 
*[http://milky.manishsinha.net/2009/03/29/working-with-libnotify/ C サンプル]
 
*[http://hashbang.fr/tutoriel-notify.html Python サンプル] (フランス語の記事)
 

2025年7月3日 (木) 17:41時点における最新版

関連記事

デスクトップ通知は、非同期に特定のイベントをユーザーに通知する小さくて控えめなポップアップダイアログです。

Libnotify

libnotifynotify-send(1) ユーティリティや GTKQt のアプリケーションのサポートを提供する Desktop Notifications Specification の実装で特定のデスクトップに依存していません。この実装はすでに EvolutionPidgin といった多くのオープンソースアプリによって使用されています。

ヒント: 利用可能なアイコンに関する概要は仕様にあります。

通知サーバー

libnotify で送られた通知を受け取るには、通知サーバーが必要です。

ビルトイン

CinnamonDeepinEnlightenmentGNOMEGNOME Flashback は独自の実装を用いて通知を表示します。これらの通知サーバーはアプリケーションからの通知を DBus 経由で受け取るためにログイン時に自動的に起動されるので、他の通知サーバーに置き換えることができない場合があります。

KDE Plasma では、システムトレイの設定を開き、システムサービスの項目から Notifications の横にあるドロップダウンを Disabled に変更することで、ビルトインの通知サーバーを無効化することができます。その後システム設定メニューで System / Autostart のところに新しい自動起動アプリケーションを追加することで、所望の通知サーバーを追加することができます。変更を適用するには一旦ログアウトし、ログインし直す必要があります。

スタンドアロン

他のデスクトップ環境では、通知サーバーは手動でインストールし、XDG Autostart などを使って起動する必要があります。

あるいは、通知サーバーを D-Bus サービスにすることで、通知サーバーを初めて呼び出した時に自動的に起動させることもできます。ほとんどの通知サーバーにはすでに dbus サービスが /usr/share/dbus-1/services に用意されています。notification-daemon などの一部の実装では、ユーザー D-Bus サービスのディレクトリ ($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

アプリケーションが通知を送ると (つまり org.freedesktop.Notifications にシグナルを送ると)、/usr/lib/notification-daemon-1.0/notification-daemon がまだアクティブ化されていない場合、D-Bus によってアクティブ化されます。

通知サーバーは以下から選ぶことができます:

https://github.com/phuhl/linux_notification_center || deadd-notification-centerAUR
  • Dunstdwm などのミニマルなウィンドウマネージャにうってつけの Linux 用の最小通知デーモン。1.6 から Wayland サポートがあります。
https://dunst-project.org/ || dunst
  • fnott — wlroots ベースのコンポジタのための軽量な Wayland 向けの通知デーモン。キーボードによって操作可能。
https://codeberg.org/dnkl/fnott || fnott
  • LXQt Notification DaemonLXQt の通知サーバー。
https://github.com/lxde/lxqt-notificationd || lxqt-notificationd
  • makoWayland 向けの軽量な通知サーバー。現時点では Swayriver 上で動作します。
https://github.com/emersion/mako || mako
  • MATE Notification DaemonMATE の通知サーバー。
https://github.com/mate-desktop/mate-notification-daemon/ || mate-notification-daemon
  • Notification Daemon — オリジナルの通知サーバー。
https://gitlab.gnome.org/Archive/notification-daemon || notification-daemon
/usr/lib/notification-daemon-1.0/notification-daemon で手動で起動できます。
  • Notify OSD — Unity の通知サーバー。
https://launchpad.net/notify-osd || notify-osd
  • statnot — 小さくて軽量な通知デーモン。ルートウィンドウのタイトルや標準出力、FIFO パイプなどに通知を出力できるので、タイル型ウィンドウマネージャと相性がとても良いです。
https://github.com/halhen/statnot || statnotAUR
  • swayncSway のためのシンプルな GTK ベースの通知デーモン。
https://github.com/ErikReider/SwayNotificationCenter || swaync
  • twmn — タイル型ウィンドウマネージャ向けの通知システム。
https://github.com/sboli/twmn || twmn-gitAUR
  • wired — レイヤーブロックのカスタマイズ性が高い軽量な通知デーモン。Rust で書かれている。
https://github.com/Toqozz/wired-notify || wiredAUR
  • Xfce Notification DaemonXfce の通知サーバー。
https://docs.xfce.org/apps/notifyd/start || xfce4-notifyd
/usr/lib/xfce4/notifyd/xfce4-notifyd で手動で起動できます。
ヒント: xfce4-notifyd を設定するには、ターミナルから次のコマンドを実行してください: xfce4-notifyd-config

ヒントとテクニック

他のユーザーに通知を送る

systemd-run(1) を使うことで (例えば root として実行されたバックグラウンドのスクリプトなどから)、他のユーザーのセッションに入り、そのユーザーに通知を送ることができます:

# systemd-run --machine=target_user@.host --user notify-send 'Hello world!' 'This is an example notification.'

もう一つの方法は systembus-notify です。以下のコマンドは、systembus-notify をセッション内に実行しているすべてのユーザーに通知を表示します:

$ dbus-send --system / net.nuetzlich.SystemNotifications.Notify 'string:Hello world!' 'string:This is an example notification.'
ヒント: systemd#サービスの失敗を通知する で説明されているようにして、上記のコマンドを ExecStart コマンドで実行することで、失敗したサービスを通知で知らせることもできます。

以前の通知を置き換える

通知 ID がわかっている場合は、その通知を置き換えることができます。新しい通知リクエストで同じ ID が指定されている場合、常に古い通知が置き換えられます。(上記の libnotify バインディングはこれを自動的に処理します。) 残念ながら、notify-send はこの ID を報告しないため、CLI でこれを行うには代替ツールが必要です。有効な CLI ツールの 1 つは、notify-send.py Python スクリプトです。これは、追加の ID レポート機能と置換機能を備えた Notify-Send 構文を提供します。

ただし、一部の 通知サーバー (Notify-OSD など) では、notify-send で string:x-canonical-private-synchronous: ヒントを使用して同じ結果を得ることができます。

たとえば、時間を表示する通知を取得するには:

while true; do
  date=$(date)
  notify-send "$date" -h string:x-canonical-private-synchronous:my-notification
  sleep 1
done

ボタンを追加する、通知の閉じる/クリックをリッスンする

notify-send.py スクリプトを使用すると、アクションを使用してボタンを表示したり通知のデフォルトアクション (通常、ユーザーが通知をクリックした時に発生します) やクローズアクションをリッスンすることができます。action-icons ヒントが true に設定されており、通知デーモンがこれをサポートしている場合、ボタンにはテキストの代わりにアイコンが表示されます。スクリプトは、対応するイベントが発生したときに、コマンドラインにアクション ID または "close" を出力します。デフォルトのアクション (クリック時) をリッスンするには、アクション識別子 "default" を使用する必要があります。

ボタンにアイコンを表示する例:

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

D-Bus サービスが同梱されている通知サーバーが複数ある場合

スタンドアロン章で説明した通り、ユーザーは D-Bus サービスを作成して通知サーバーを自動的に起動させることができます。しかし、一部の通知サーバーにはすでに D-Bus サービスファイルが同梱されています。複数の通知サーバーをインストールしていて、それらのうちどれかにサービスファイルが含まれている場合、問題が発生します。例えば、dunstmako の両方をインストールして、使用するサーバーを明示的に指定しなかった場合、D-Bus はどちらか一方を勝手に選びます。そうならないようにするには、org.freedesktop.Notifications.service (#スタンドアロン を参照) を作成して、使用したいサービスを指定することで、使用されるサービスをオーバーライドすることができます。ファイルの作成後は、セッションを再起動してください。

トラブルシューティング

アプリケーションがちょうど 1 分間ハングする

アプリケーションが通知を表示しようとした際にハングしてしまう場合、通知サービスが自身の可用性を誤って D-Bus サービス経由で広告してしまっているのかもしれません。

たとえば、ユーザーが最近、plasma-workspace を必要とする KDE コンポーネントをインストールしたが、まだ XFCE を実行しているとします。この場合、KDE ​​通知機能が優先されますが、ユーザーはそれを実行していません。アプリケーションはサービスの待機中にハングし、タイムアウト後に xfce4-notifyd にフォールバックすることになります。

最もわかりやすいハングは、通知インディケータの調整スクロールでしょう。

このような場合、2つの通知ハンドラーが存在するはずです:

$ find /usr/share/dbus-1/services/ -name '*Notif*'
org.kde.plasma.Notifications.service
org.xfce.xfce4-notifyd.Notifications.service

journal を見ると、これら2つのうちどちら一方が定期的に1分のタイムアウトで失敗しています:

# journalctl -g notif
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

#D-Bus サービスが同梱されている通知サーバーが複数ある場合 で説明されているように、使いたいサービスを指定すれば、問題は解決します。

参照

翻訳ステータス: このページは en:Desktop notifications の翻訳バージョンです。最後の翻訳日は 2025-07-03 です。もし英語版に 変更 があれば、翻訳の同期を手伝うことができます。