如何使用Java特定于操作系统的通知?

时间:2019-12-03 22:08:08

标签: java notifications systray platform-specific

我不是行业程序员(我拥有的所有Java知识都来自Hard Knocks学院)。请原谅我要问的愚蠢问题,并适当回答。

我正在使用的Java应用程序使用与平台无关的非常多的错误通知(例如成功下载文件时)。我想使用平台感知的通知。在Linux上发出通知的代码非常简单:

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

在Mac上,它有些复杂,但仍然可以实现:

interface NsUserNotificationsBridge extends Library {
    NsUserNotificationsBridge instance = (NsUserNotificationsBridge)
            Native.loadLibrary("/usr/local/lib/NsUserNotificationsBridge.dylib", NsUserNotificationsBridge.class);

    public int sendNotification(String title, String subtitle, String text, int timeoffset);
}

它需要一个可从以下github存储库获得的dylib:https://github.com/petesh/OSxNotificationCenter

Windows方式如下:

import java.awt.*;
import java.awt.TrayIcon.MessageType;

public class TrayIconDemo {

    public static void main(String[] args) throws AWTException {
        if (SystemTray.isSupported()) {
            TrayIconDemo td = new TrayIconDemo();
            td.displayTray();
        } else {
            System.err.println("System tray not supported!");
        }
    }

    public void displayTray() throws AWTException {
        //Obtain only one instance of the SystemTray object
        SystemTray tray = SystemTray.getSystemTray();

        //If the icon is a file
        Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png"));

        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        //Let the system resize the image if needed
        trayIcon.setImageAutoSize(true);
        //Set tooltip text for the tray icon
        trayIcon.setToolTip("System tray icon demo");
        tray.add(trayIcon);

        trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
    }
}

重点是,我希望这些代码片段仅在适当的平台上执行 ;我不希望Java在Windows上编译GTK方法,因为它的依赖项不存在。

如何使Java识别它,就像“嘿,我正在为Mac系统编译,所以我正在使用Mac版本的代码。”

1 个答案:

答案 0 :(得分:0)

为了拥有简单而干净的东西而没有其他依赖关系,我会放弃所有本机库,而是依靠我确信可以保证(或至少有可能)在每个程序上使用的本机程序各自的系统:

String title = "Hello world!";
String message = "This is an example notification.";
Image image = ImageIO.read(getClass().getResource("icon.png"));

String os = System.getProperty("os.name");
if (os.contains("Linux")) {
    ProcessBuilder builder = new ProcessBuilder(
        "zenity",
        "--notification",
        "--title=" + title,
        "--text=" + message);
    builder.inheritIO().start();
} else if (os.contains("Mac")) {
    ProcessBuilder builder = new ProcessBuilder(
        "osascript", "-e",
        "display notification \"" + message + "\""
            + " with title \"" + title + "\"");
    builder.inheritIO().start();
} else if (SystemTray.isSupported()) {
    SystemTray tray = SystemTray.getSystemTray();

    TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
    trayIcon.setImageAutoSize(true);
    tray.add(trayIcon);

    trayIcon.displayMessage(title, message, TrayIcon.MessageType.INFO);
}