桌面气泡通知 (Desktop notifications )

效果图:


守护进程中调用不生效,因为守护进程对图形和终端有一定的限制。可以打印信息到文件里,或者写syslog


Libnotify

Libnotify 是Qt和GTK图形的一个扩展,已经广泛的应用于许多开源程序,如:Evolution和Pidgin。Libnotify会在安装libnotify时被安装。 

使用libnotify必须安装 notification server.

Notification servers

内置

下面的桌面环境中使用自己实现的显示通知,你不能代替他们。他们的通知服务器在登录时自动启动通过的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.

独立

在其他的桌面环境中, notification server 需要通过 WM's/DE's "autostart" 选项启动. (它可以通过的DBus第一个i会话被推出,但是这不是很理想,因为它需要全局配置.)

You can choose one of the following implementations:

https://github.com/p12tic/awn-extras ||  awn-extras-applets AUR
  • 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
  • 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 ||  statnot AUR
  • twmn — Notification system for tiling window managers.
https://github.com/sboli/twmn ||  twmn-git AUR
  • 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.

在程序中用法

在许多编程语言中,您可以通过 GObject-Introspection或bindings很容易地编写你自己的libnotify显示消息,也可以简单地使用bash。

下面是实现“Hello world”气泡消息的简单示例

Bash

hello_world.sh
#!/bin/bash
notify-send 'Hello world!' 'This is an example notification.' --icon=dialog-information
Tip:
  • 可用的内置图标概述e...http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html(也可以自定义图标)
  • 要以root运行一个后台脚本发送桌面通知(更换用户与用户运行的X):
    # sudo -u X_user DISPLAY=:0 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>
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++

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

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

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#

  • 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 Notification ("Hello world!","This is an example notification.","dialog-information")
	Hello.show ()

Groovy

  • Dependencies: groovyjava-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()

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

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

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

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

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

另见

  • Libnotify 手册:https://developer.gnome.org/libnotify/
  • 实例:http://milky.manishsinha.net/2009/03/29/working-with-libnotify/
  • Python 实例(french article):https://hashbang.fr/tutoriel-notify.html
  • 官网资料:https://wiki.archlinux.org/index.php/Desktop_notifications

  • 气泡中图标资料:http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值