package main import ( "encoding/json" "fmt" "log" "sync" "time" "github.com/godbus/dbus/v5" ) type notificationServer struct{} type Notification struct { AppName string ReplacesId uint32 Icon string Summary string Body string Actions []string ExpireTimeout int32 } // {"text": "$text", "alt": "$alt", "tooltip": "$tooltip", "class": "$class", "percentage": $percentage } type Format struct { Text string `json:"text"` } func (s notificationServer) Notify(appName string, replacesId uint32, appIcon string, summary string, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (uint32, *dbus.Error) { n := Notification{ AppName: appName, ReplacesId: replacesId, Icon: appIcon, Summary: summary, Body: body, Actions: actions, ExpireTimeout: expireTimeout, } log.Println(n) way := Format{ Text: fmt.Sprintf("%s: %s", n.Summary, n.Body), } res, err := json.Marshal(way) if err != nil { panic(err) } defer func() { wg := sync.WaitGroup{} wg.Add(1) go clearNotification(n.ReplacesId, n.ExpireTimeout) wg.Done() }() fmt.Printf("%s\n", res) return 0, nil } func clearNotification(id uint32, t int32) { timeout, err := time.ParseDuration(fmt.Sprintf("%vms", t)) if t == -1 || err != nil { timeout = time.Duration(5000 * time.Millisecond) } time.Sleep(timeout) log.Println("close the notification", id) fmt.Printf("\n") } func (s notificationServer) CloseNotification(id uint32) { log.Println("dbus called CloseNotification", id) } func (s notificationServer) GetCapabilities() ([]string, *dbus.Error) { return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil } func (s notificationServer) GetServerInformation() (string, string, string, string, *dbus.Error) { return "notice", "meatbag.se", "1.0", "1.2", nil } func main() { conn, err := dbus.ConnectSessionBus() if err != nil { panic(err) } defer conn.Close() f := notificationServer{} conn.Export(f, "/org/freedesktop/Notifications", "org.freedesktop.Notifications") reply, err := conn.RequestName("org.freedesktop.Notifications", dbus.NameFlagDoNotQueue) if err != nil { panic(err) } if reply != dbus.RequestNameReplyPrimaryOwner { log.Fatal("name already taken") } select {} }