From cb21ee99186e1594df661a117762c640ad5c8435 Mon Sep 17 00:00:00 2001 From: lroyia Date: Thu, 4 Dec 2025 17:29:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0Go=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E6=8F=90=E9=86=92=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现跨平台系统通知功能(Windows/macOS/Linux) - 支持命令行参数传递消息和标题 - 添加完整的使用文档和示例 --- README.md | 71 ++++++++++++++++++++++++++++++++++++++++++- go.mod | 3 ++ main.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 main.go diff --git a/README.md b/README.md index d68a053..6a33d3b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,72 @@ # location-notice -本地提醒小程序 \ No newline at end of file +本地提醒小程序 + +## 功能介绍 + +这是一个用Go语言开发的本地提醒功能,可以通过命令行参数传入消息,然后调用系统通知接口弹出系统消息。 + +## 支持平台 + +- Windows +- macOS +- Linux + +## 安装和运行 + +### 前提条件 + +需要安装Go语言环境(1.21或更高版本) + +### 编译运行 + +```bash +# 直接运行 +go run main.go -message "你的提醒消息" -title "提醒标题" + +# 编译为可执行文件 +go build -o location-notice main.go + +# 运行编译后的程序 +./location-notice -message "你的提醒消息" -title "提醒标题" +``` + +## 使用方法 + +### 基本用法 + +```bash +go run main.go -message "该喝水了!" +``` + +### 指定标题 + +```bash +go run main.go -message "会议时间到了" -title "工作提醒" +``` + +### 参数说明 + +- `-message`: 通知消息内容(必需) +- `-title`: 通知标题(可选,默认为"提醒") + +## 示例 + +```bash +# 健康提醒 +go run main.go -message "站起来活动一下,保护你的腰部" -title "健康提醒" + +# 工作提醒 +go run main.go -message "15分钟后有重要会议" -title "会议提醒" + +# 生活提醒 +go run main.go -message "记得今天要交水电费" -title "生活提醒" +``` + +## 技术实现 + +- 使用Go标准库的`flag`包解析命令行参数 +- 根据不同操作系统调用相应的系统通知命令: + - Windows: 使用PowerShell的Toast通知 + - macOS: 使用osascript调用系统通知 + - Linux: 使用notify-send命令 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c41bcb1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module location-notice + +go 1.21 \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..ccbf2d4 --- /dev/null +++ b/main.go @@ -0,0 +1,90 @@ +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "strings" +) + +func main() { + // 定义命令行参数 + var message string + var title string + + flag.StringVar(&message, "message", "", "通知消息内容") + flag.StringVar(&title, "title", "提醒", "通知标题") + flag.Parse() + + // 检查是否提供了消息参数 + if message == "" { + fmt.Println("请提供消息内容,使用 -message 参数") + fmt.Println("用法: go run main.go -message \"你的消息\" [-title \"标题\"]") + os.Exit(1) + } + + // 发送系统通知 + err := sendNotification(title, message) + if err != nil { + fmt.Printf("发送通知失败: %v\n", err) + os.Exit(1) + } + + fmt.Println("通知已发送") +} + +// sendNotification 发送系统通知 +func sendNotification(title, message string) error { + // 检测操作系统并使用相应的通知命令 + switch os := strings.ToLower(os.Getenv("OS")); os { + case "windows_nt", "windows": + return sendWindowsNotification(title, message) + case "darwin": + return sendMacNotification(title, message) + default: + // 假设是Linux系统 + return sendLinuxNotification(title, message) + } +} + +// sendWindowsNotification 发送Windows系统通知 +func sendWindowsNotification(title, message string) error { + // 使用PowerShell的Toast通知 + powershellCmd := fmt.Sprintf( + "Add-Type -AssemblyName System.Windows.Forms; "+ + "$notify = New-Object System.Windows.Forms.NotifyIcon; "+ + "$notify.Icon = [System.Drawing.SystemIcons]::Information; "+ + "$notify.BalloonTipTitle = '%s'; "+ + "$notify.BalloonTipText = '%s'; "+ + "$notify.Visible = $true; "+ + "$notify.ShowBalloonTip(3000); "+ + "Start-Sleep -Seconds 3", + escapePowerShellString(title), + escapePowerShellString(message), + ) + + cmd := exec.Command("powershell.exe", "-Command", powershellCmd) + return cmd.Run() +} + +// sendMacNotification 发送macOS系统通知 +func sendMacNotification(title, message string) error { + cmd := exec.Command("osascript", "-e", + fmt.Sprintf(`display notification "%s" with title "%s"`, message, title)) + return cmd.Run() +} + +// sendLinuxNotification 发送Linux系统通知 +func sendLinuxNotification(title, message string) error { + cmd := exec.Command("notify-send", title, message) + return cmd.Run() +} + +// escapePowerShellString 转义PowerShell字符串中的特殊字符 +func escapePowerShellString(s string) string { + s = strings.ReplaceAll(s, "'", "''") + s = strings.ReplaceAll(s, "`", "``") + s = strings.ReplaceAll(s, "\"", "`\"") + return s +}