You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用Java为Windows 10 Toast通知添加可触发动作的按钮

嘿,这个需求完全能实现!我来给你拆解两种可行方案——纯Java实现,以及Java结合XML的方式,帮你搞定带自定义按钮的Windows 10 Toast通知。

方案1:纯Java实现带按钮的Windows 10 Toast通知

Windows的Toast通知本质是基于XML结构的,我们可以在Java里直接构造这个XML内容,然后通过调用PowerShell命令来触发通知。步骤如下:

  1. 构造Toast通知的XML结构
    带按钮的Toast核心是<actions>节点,每个<action>对应一个按钮:content是按钮文字,arguments是点击后传递的参数,activationType设为Foreground会在点击时启动你的应用。示例XML如下:

    <toast>
      <visual>
        <binding template="ToastGeneric">
          <text>自定义通知标题</text>
          <text>点击按钮触发不同动作</text>
        </binding>
      </visual>
      <actions>
        <action content="打开应用" arguments="open_app" activationType="Foreground"/>
        <action content="执行操作" arguments="do_action" activationType="Foreground"/>
      </actions>
    </toast>
    
  2. Java代码调用PowerShell发送通知
    ProcessBuilder执行PowerShell命令,把构造好的XML传递给Windows的Toast通知管理器:

    import java.io.IOException;
    
    public class WindowsToastSender {
        public static void main(String[] args) throws IOException {
            // 构造Toast XML字符串
            String toastXml = "<toast>" +
                    "  <visual>" +
                    "    <binding template=\"ToastGeneric\">" +
                    "      <text>我的自定义通知</text>" +
                    "      <text>点击下方按钮执行对应操作</text>" +
                    "    </binding>" +
                    "  </visual>" +
                    "  <actions>" +
                    "    <action content=\"打开主程序\" arguments=\"launch_main\" activationType=\"Foreground\"/>" +
                    "    <action content=\"执行清理\" arguments=\"run_cleanup\" activationType=\"Foreground\"/>" +
                    "  </actions>" +
                    "</toast>";
    
            // 调用PowerShell发送通知,替换成你的AppID(随便定义一个唯一标识即可)
            String appId = "com.yourcompany.yourjavapp";
            ProcessBuilder pb = new ProcessBuilder("powershell.exe",
                    "-Command",
                    String.format("$xml = @'%s'@; [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null; $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastGeneric); $xmlDoc = [xml]$xml; $node = $template.ImportNode($xmlDoc.toast, $true); $template.AppendChild($node); $toast = [Windows.UI.Notifications.ToastNotification]::new($template); $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('%s'); $notifier.Show($toast);", toastXml, appId));
    
            pb.start();
        }
    }
    
方案2:Java结合外部XML文件实现

如果你的通知结构比较复杂,或者需要频繁修改样式,把XML抽离到外部文件会更便于维护:

  1. 创建外部XML文件
    新建toast_template.xml,内容就是方案1里的XML结构。

  2. Java读取XML文件并发送通知

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    public class WindowsToastFromXml {
        public static void main(String[] args) throws IOException {
            // 读取外部XML文件
            String toastXml = new String(Files.readAllBytes(Paths.get("toast_template.xml")));
    
            // 同样调用PowerShell发送,替换成你的AppID
            String appId = "com.yourcompany.yourjavapp";
            ProcessBuilder pb = new ProcessBuilder("powershell.exe",
                    "-Command",
                    String.format("$xml = @'%s'@; [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null; $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastGeneric); $xmlDoc = [xml]$xml; $node = $template.ImportNode($xmlDoc.toast, $true); $template.AppendChild($node); $toast = [Windows.UI.Notifications.ToastNotification]::new($template); $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('%s'); $notifier.Show($toast);", toastXml, appId));
    
            pb.start();
        }
    }
    
处理按钮点击事件

当用户点击通知按钮时,Windows会启动你的Java应用,并把<action>里的arguments作为命令行参数传递过来。你可以在main方法里监听这些参数,执行对应逻辑:

public static void main(String[] args) throws IOException {
    // 先处理通知按钮传递的参数
    if (args.length > 0) {
        String action = args[0];
        switch (action) {
            case "launch_main":
                System.out.println("触发了打开主程序的动作!");
                // 这里写你的业务逻辑,比如打开主窗口
                break;
            case "run_cleanup":
                System.out.println("触发了执行清理的动作!");
                // 这里写清理逻辑
                break;
        }
        return;
    }

    // 如果没有参数,说明是主动启动,发送通知
    String toastXml = "..."; // 构造或读取XML
    // 发送通知的代码...
}
关键注意事项
  • AppID的作用:Windows需要一个唯一的AppID来识别通知来源,你可以随便定义一个(比如com.yourteam.yourapp),但要保持一致。
  • 注册应用(可选):如果希望通知点击后能正确唤起你的应用,建议在Windows注册表中注册AppID,并创建对应快捷方式(放在开始菜单目录下),这样Windows能正确关联通知和你的应用。

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

火山引擎 最新活动