go-nativenotify

Native notifications in Go
Log | Files | Refs | README | LICENSE

main.go (2053B)


      1 package main
      2 
      3 import (
      4 	"flag"
      5 	"fmt"
      6 	"log/slog"
      7 	"os"
      8 	"path/filepath"
      9 	"runtime"
     10 	"time"
     11 
     12 	"gioui.org/app"
     13 	"git.sr.ht/~jackmordaunt/go-nativenotify"
     14 )
     15 
     16 func main() {
     17 	slog.SetLogLoggerLevel(slog.LevelDebug)
     18 	slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil)))
     19 
     20 	var (
     21 		title   string
     22 		body    string
     23 		icon    string
     24 		payload string
     25 	)
     26 
     27 	flag.StringVar(&title, "title", "title", "title text of notification")
     28 	flag.StringVar(&body, "body", "body", "body text of notification")
     29 	flag.StringVar(&icon, "icon", "./puzzle.png", "path to icon file for notification")
     30 	flag.StringVar(&payload, "payload", "example-payload", "data to be returned upon activation")
     31 	flag.Parse()
     32 
     33 	if abs, err := filepath.Abs(icon); err == nil {
     34 		icon = abs
     35 	}
     36 
     37 	slog.Info("icon", "path", icon)
     38 
     39 	if err := nativenotify.Setup(nativenotify.Config{
     40 		Windows: nativenotify.WindowsConfig{
     41 			AppID:    "notify-test",
     42 			GUID:     "{B5E38D62-B912-486C-96E3-6FAD1082B73D}",
     43 			IconPath: icon,
     44 		},
     45 		Linux: nativenotify.LinuxConfig{
     46 			AppName: "notify-test",
     47 			AppIcon: icon,
     48 		},
     49 		Darwin: nativenotify.DarwinConfig{},
     50 	}); err != nil {
     51 		slog.Error("setting up notification subsystem", "err", err)
     52 	}
     53 
     54 	if err := nativenotify.Push(nativenotify.Notification{
     55 		Title: title,
     56 		Body:  body,
     57 		Icon:  icon,
     58 		TextActions: []nativenotify.TextAction{
     59 			{
     60 				ID:              "reply",
     61 				Title:           "Reply",
     62 				PlaceholderHint: "type here...",
     63 				ButtonLabel:     "Send",
     64 			},
     65 		},
     66 		ButtonActions: []nativenotify.ButtonAction{
     67 			{
     68 				ID:        "like",
     69 				LabelText: "Like",
     70 				Value:     "@jack",
     71 			},
     72 		},
     73 		Callback: func(action, value string) {
     74 			slog.Info("callback", "action", action, "value", value)
     75 		},
     76 	}); err != nil {
     77 		slog.Error("pushing notification", "err", err)
     78 	}
     79 
     80 	fmt.Printf("callback invocation will log, quit when done (ctr-c)\n")
     81 	wait()
     82 }
     83 
     84 func wait() {
     85 	switch runtime.GOOS {
     86 	case "windows":
     87 		// Avoid the deadlock panic on Windows.
     88 		<-time.NewTimer(1<<63 - 1).C
     89 	case "darwin":
     90 		app.Main()
     91 	default:
     92 		select {}
     93 	}
     94 }