Golang 实现调用 Windows 10 通知

    xiaoxiao2023-10-11  189

    使用到的库:https://github.com/go-toast/toast

    toast 库实现原理很简单,用 Go 的 exec.Command 调用临时生成的包含通知代码的 Powershell 脚本

    示例代码:

    package main import ( "github.com/go-toast/toast" "log" ) func main() { notification := toast.Notification{ AppID: "Microsoft.Windows.Shell.RunDialog", Title: "标题", Message: "这是消息内容,等等。。。", Icon: "C:\\path\\to\\your\\logo.png", // 文件必须存在 Actions: []toast.Action{ {"protocol", "按钮1", "https://www.google.com/"}, {"protocol", "按钮2", "https://github.com/"}, }, } err := notification.Push() if err != nil { log.Fatalln(err) } }

    需要注意,最新版本的 win10 需要提供一个有效的 APPID 才能显示通知,这里使用“运行”的 APPID

    最终效果:

    更多有关 Win10 通知的内容请参考:https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts

    最新回复(0)