feat: 添加Go语言本地提醒功能

- 实现跨平台系统通知功能(Windows/macOS/Linux)
- 支持命令行参数传递消息和标题
- 添加完整的使用文档和示例
This commit is contained in:
黎润豪 2025-12-04 17:29:27 +08:00
parent 0fb9adfbac
commit cb21ee9918
3 changed files with 163 additions and 1 deletions

View File

@ -1,3 +1,72 @@
# location-notice
本地提醒小程序
本地提醒小程序
## 功能介绍
这是一个用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命令

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module location-notice
go 1.21

90
main.go Normal file
View File

@ -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
}