Linux 使用go语言通过D-Bus发送桌面通知

使用dbus.SessionBus()和DBus建立连接

conn, err := dbus.SessionBus()
if err != nil {
	panic(err)
}

通过调用org.freedesktop.Notifications.Notify方法发送桌面通知

call := conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications").Call(
	"org.freedesktop.Notifications.Notify", 0,
	appName, idNum, icon, title, text, actions, hints, expireTime,
)
if call.Err != nil {
	panic(call.Err)
}

Notify参数解释如下:

appName 显示在锁屏界面的应用程序名字
icon 图标文件路径或名称, 一些常见的图标名称有:

  • dialog-information 消息或提示对话框
  • dialog-warning 警告对话框
  • dialog-error 错误对话框

title 通知的标题
text 通知的具体内容
expireTime 通知显示的过期时间

完整代码如下

package main

import (
	"github.com/godbus/dbus/v5"
	"time"
)

func main() {
	conn, err := dbus.SessionBus()
	if err != nil {
		panic(err)
	}

	// 发送通知
	appName := "My Application"
	idNum := uint32(0)
	icon := ""  // 图标文件路径或名称
	title := "Notification Title"
	text := "This is the notification message."
	actions := []string{}  // 可选操作列表
	hints := map[string]dbus.Variant{}  // 可选附加信息字典
	expireTime := int32(time.Now().Unix() + 5)  // 过期时间,单位秒

	call := conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications").Call(
		"org.freedesktop.Notifications.Notify", 0,
		appName, idNum, icon, title, text, actions, hints, expireTime,
	)
	if call.Err != nil {
		panic(call.Err)
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值