golang实现freedesktop桌面通知规范的插件库go-notify的使用

golang实现freedesktop桌面通知规范的插件库go-notify的使用

go-notify是一个实现了Gnome DBus通知规范的Golang库,可以用于在Linux桌面环境中显示通知。

基本用法

显示简单通知

// 创建新通知
ntf := notify.NewNotification("测试通知", "这是一个测试通知")
// 显示通知
if _, err := ntf.Show(); err != nil {
    return
}

带图标的通知

// 创建新通知
ntf := notify.NewNotification("测试通知", "这是一个带图标的测试通知")
// 设置图标(使用标准图标名称)
ntf.AppIcon = "network-wireless"
// 显示通知
if _, err := ntf.Show(); err != nil {
    return
}

永不过期的通知

// 创建新通知
ntf := notify.NewNotification("测试通知", "这是一个永不过期的测试通知")
// 设置通知永不过期
ntf.Timeout = notify.ExpiresNever
// 显示通知
if _, err := ntf.Show(); err != nil {
    return
}

带声音的通知

// 创建新通知
ntf := notify.NewNotification("测试通知", "这是一个带声音的测试通知")
// 初始化提示字典
ntf.Hints = make(map[string]interface{})
// 设置声音文件路径
ntf.Hints[notify.HintSoundFile] = "/home/my-username/sound.oga"
// 显示通知
if _, err := ntf.Show(); err != nil {
    return
}

完整示例

下面是一个完整的示例程序,展示了如何使用go-notify库的各种功能:

package main

import (
	"github.com/TheCreeper/go-notify"
)

func main() {
	// 示例1: 基本通知
	basicNotification := notify.NewNotification("基本通知", "这是一个简单的桌面通知示例")
	if _, err := basicNotification.Show(); err != nil {
		panic(err)
	}

	// 示例2: 带图标的通知
	iconNotification := notify.NewNotification("图标通知", "这是一个带网络图标的通知")
	iconNotification.AppIcon = "network-wireless"
	if _, err := iconNotification.Show(); err != nil {
		panic(err)
	}

	// 示例3: 永不过期的通知
	persistentNotification := notify.NewNotification("持久通知", "这个通知不会自动消失")
	persistentNotification.Timeout = notify.ExpiresNever
	if _, err := persistentNotification.Show(); err != nil {
		panic(err)
	}

	// 示例4: 带声音的通知
	soundNotification := notify.NewNotification("声音通知", "这个通知会播放声音")
	soundNotification.Hints = make(map[string]interface{})
	soundNotification.Hints[notify.HintSoundFile] = "/usr/share/sounds/freedesktop/stereo/message.oga"
	if _, err := soundNotification.Show(); err != nil {
		panic(err)
	}
}

注意:使用前需要先安装go-notify库:

go get github.com/TheCreeper/go-notify

更多关于golang实现freedesktop桌面通知规范的插件库go-notify的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang实现freedesktop桌面通知规范的插件库go-notify的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用go-notify实现Freedesktop桌面通知

go-notify是一个Golang库,用于实现Freedesktop桌面通知规范(D-Bus通知)。它允许你的Go应用程序发送桌面通知到兼容的桌面环境(如GNOME、KDE等)。

安装

首先安装go-notify库:

go get github.com/mqu/go-notify

基本使用示例

package main

import (
	"github.com/mqu/go-notify"
	"time"
)

func main() {
	// 初始化通知系统
	notify.Init("My Go Application")

	// 创建一个新通知
	notification := notify.NotificationNew(
		"通知标题",                  // 标题
		"这是一条来自Go程序的通知消息",       // 正文内容
		"dialog-information",      // 图标名称(可选)
	)

	// 设置超时时间(毫秒)
	notification.SetTimeout(5000) // 5秒后自动关闭

	// 添加操作按钮(可选)
	notification.AddAction("default", "默认操作", func(n *notify.Notification, action string) {
		println("用户点击了默认操作")
	})

	notification.AddAction("cancel", "取消", func(n *notify.Notification, action string) {
		println("用户点击了取消")
	})

	// 显示通知
	if err := notification.Show(); err != nil {
		println("显示通知失败:", err.Error())
		return
	}

	// 保持程序运行足够长时间以显示通知
	time.Sleep(6 * time.Second)

	// 关闭通知(可选)
	notification.Close()

	// 清理资源
	notify.UnInit()
}

高级功能

1. 设置紧急级别

notification := notify.NotificationNew("紧急通知", "系统即将崩溃!", "dialog-warning")
notification.SetUrgency(notify.UrgencyCritical) // 紧急级别: Low, Normal, Critical
notification.Show()

2. 使用自定义图标

notification := notify.NotificationNew("自定义图标", "使用本地文件作为图标", "")
notification.SetIconFromPixbuf("/path/to/icon.png") // 使用本地图片文件
notification.Show()

3. 处理通知点击事件

notification := notify.NotificationNew("可点击通知", "点击我会执行操作", "")
notification.SetCallback(func(n *notify.Notification, action string) {
    if action == "default" { // 默认点击行为
        println("通知被点击了!")
    }
})
notification.Show()

4. 更新现有通知

notification := notify.NotificationNew("初始通知", "这是第一条消息", "")
notification.Show()

time.Sleep(2 * time.Second)

// 更新通知内容
notification.Update("更新后的通知", "这是更新后的消息", "")
notification.Show()

注意事项

  1. D-Bus依赖:go-notify需要系统有D-Bus服务运行,这在大多数Linux桌面环境中默认已安装。

  2. 桌面环境兼容性:虽然Freedesktop通知规范被广泛支持,但不同桌面环境可能有细微差异。

  3. 错误处理:如果通知系统不可用(如在无GUI环境中),Show()会返回错误。

  4. 资源清理:使用完毕后调用UnInit()释放资源。

  5. 超时设置:如果不设置超时,某些桌面环境会使用默认超时,而有些则会一直显示直到用户关闭。

完整示例:带重试机制的通知发送

package main

import (
	"github.com/mqu/go-notify"
	"time"
)

func sendNotificationWithRetry(title, body, icon string, retries int) error {
	notify.Init("My Go App")
	defer notify.UnInit()

	var lastErr error
	
	for i := 0; i < retries; i++ {
		n := notify.NotificationNew(title, body, icon)
		n.SetTimeout(3000)
		
		if err := n.Show(); err != nil {
			lastErr = err
			time.Sleep(500 * time.Millisecond) // 等待后重试
			continue
		}
		
		time.Sleep(4 * time.Second) // 确保通知显示时间
		return nil
	}
	
	return lastErr
}

func main() {
	if err := sendNotificationWithRetry(
		"重要消息",
		"您的系统更新已准备好安装",
		"system-software-update",
		3,
	); err != nil {
		println("无法发送通知:", err.Error())
	}
}

go-notify提供了一种简单的方式来为你的Go应用程序添加桌面通知功能,适用于需要与用户交互的桌面应用程序。

回到顶部