使用到的库: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
本文介绍了如何使用Golang结合go-toast库来实现调用Windows 10的通知功能。核心思路是通过Go的系统调用执行含有通知代码的Powershell脚本。在最新的Windows 10系统中,展示通知需要提供有效的APPID,可以使用“运行”应用的APPID。文章还提到了更多关于Win10通知的参考资料。
306

被折叠的 条评论
为什么被折叠?



