Golang 实现调用 Windows 10 通知

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

使用到的库: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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值