You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java在macOS系统中创建桌面弹窗通知?

macOS下Java实现全局通知的几种方案

在macOS上,Java自带的SystemTray组件确实无法触发系统全局通知(仅能在托盘图标显示气泡,而非系统通知中心的通知),以下是几种可行的实现方案:

方案1:调用AppleScript触发系统通知

利用macOS自带的osascript命令,直接通过AppleScript调用系统通知中心,无需额外依赖,仅适用于macOS平台。

代码示例:

import java.io.IOException;

public class MacNativeNotification {
    public static void showNotification(String title, String subtitle, String message) {
        // 构造AppleScript脚本,处理字符串中的引号避免语法错误
        String script = String.format(
            "display notification \"%s\" with title \"%s\" subtitle \"%s\" sound name \"default\"",
            escapeQuotes(message), escapeQuotes(title), escapeQuotes(subtitle)
        );
        
        try {
            // 执行脚本
            new ProcessBuilder("osascript", "-e", script).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String escapeQuotes(String input) {
        return input.replace("\"", "\\\"");
    }

    public static void main(String[] args) {
        showNotification("项目通知", "任务完成", "您的Java项目构建已成功完成");
    }
}

注意事项:

  • 需要确保应用已获得macOS的通知权限(可在「系统设置-通知」中配置)
  • 脚本中的sound name参数可指定通知音效,传入""可关闭音效

方案2:使用JavaFX/ControlsFX实现跨平台通知

如果需要跨平台支持(同时兼容Windows、macOS),可以使用JavaFX的通知组件,或第三方库ControlsFX提供的更丰富的通知功能。

基于JavaFX原生Alert(简单实现)

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class JavaFXNotification {
    public static void showNotification(String title, String message) {
        Platform.runLater(() -> {
            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle(title);
            alert.setHeaderText(null);
            alert.setContentText(message);
            alert.initStyle(StageStyle.TRANSPARENT); // 可选:设置透明窗口风格
            alert.show();
        });
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.hide(); // 隐藏默认主窗口
    }

    public static void main(String[] args) {
        // 启动JavaFX应用线程
        Application.launch(args);
        // 延迟调用确保JavaFX环境初始化完成
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        showNotification("跨平台通知", "这是JavaFX触发的全局通知");
    }
}

基于ControlsFX(更美观的通知)

ControlsFX提供了专门的Notifications组件,支持自定义样式、图标、自动隐藏等功能,需引入第三方依赖:

Maven依赖:

<dependency>
    <groupId>org.controlsfx</groupId>
    <artifactId>controlsfx</artifactId>
    <version>11.1.2</version>
</dependency>

代码示例:

import org.controlsfx.control.Notifications;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;

public class ControlsFXNotificationDemo {
    public static void showNotification(String title, String message) {
        Platform.runLater(() -> {
            Notifications.create()
                .title(title)
                .text(message)
                .hideAfter(javafx.util.Duration.seconds(5)) // 5秒后自动隐藏
                .showInformation(); // 显示信息类通知,也可使用showWarning()/showError()
        });
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.hide();
    }

    public static void main(String[] args) {
        Application.launch(args);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        showNotification("ControlsFX通知", "支持自定义样式的跨平台通知");
    }
}

方案3:通过JNA调用macOS原生API(高度定制)

如果需要更深度的定制(比如监听通知点击事件、自定义通知内容布局),可以使用JNA直接调用macOS的UserNotifications框架。

依赖配置:

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.13.0</version>
</dependency>
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>5.13.0</version>
</dependency>

实现思路:

通过JNA封装macOS的UNUserNotificationCenter相关API,创建通知请求并发送到系统通知中心。该方案需要对macOS原生开发有一定了解,实现复杂度较高,适合有定制化需求的场景。

方案选择建议

  • 仅需macOS平台:优先选择AppleScript方案,无依赖,实现简单
  • 需要跨平台:选择JavaFX/ControlsFX方案,兼顾多系统兼容性
  • 高度定制需求:选择JNA原生API方案,但开发成本较高

内容的提问来源于stack exchange,提问作者Jelly Qwerty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:04:59