试用了多种托盘气泡提示工具,遇到一个主要问题,连续气泡提示的情况,后续的会被吞掉,不能排队或者覆盖。
利用ai写了一个,比较耗时,不过最终结果满意,特此分享。
优点:气泡提示排队显示,基本不阻塞cmd进程。
缺点:需要安装node及依赖;CPU占用稍高;产生临时文件。
安装node
安装 依赖,命令:
- npm install --save node-notifier
复制代码
toast.js 代码:
- // toast.js
- const { spawn } = require("child_process");
- const path = require("path");
- const fs = require("fs");
- const os = require("os");
- const args = process.argv.slice(2);
- const title = args[0] || "通知标题";
- const message = args[1] || "这是右下角气泡通知内容";
- const appName = args[2] || "我的气泡通知";
- // 可选图标
- const iconPath = path.join(__dirname, "icon.png");
- const iconExists = fs.existsSync(iconPath);
- // ✅ 系统临时目录
- const tempDir = os.tmpdir();
- const uniqueId = Date.now() + "_" + Math.floor(Math.random() * 10000);
- const tempScript = path.join(tempDir, `_notify_${uniqueId}.js`);
- // ✅ 注意:这里全部使用绝对路径写入(确保在 temp 里执行)
- const notifierPath = require.resolve("node-notifier");
- // 写入子脚本
- fs.writeFileSync(
- tempScript,
- `
- const notifier = require(${JSON.stringify(notifierPath)});
- const path = require('path');
- const fs = require('fs');
- const title = ${JSON.stringify(title)};
- const message = ${JSON.stringify(message)};
- const appName = ${JSON.stringify(appName)};
- const icon = ${iconExists ? JSON.stringify(iconPath) : "null"};
- notifier.notify({
- title,
- message,
- appName,
- icon: icon ? path.resolve(icon) : undefined,
- wait: false,
- timeout: 3
- });
- setTimeout(() => {
- try { fs.unlinkSync(__filename); } catch (e) {}
- }, 1500);
- `,
- "utf8"
- );
- // ✅ 用 process.execPath 启动子进程
- spawn(process.execPath, [tempScript], {
- detached: true,
- stdio: "ignore",
- shell: false,
- }).unref();
- // ✅ 主进程立即退出
- process.exit(0);
复制代码
cmd调用命令:
- node toast.js "标题" "内容" "名称(可省略)"
复制代码 |