-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.go
74 lines (62 loc) · 1.35 KB
/
notify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/url"
"os/exec"
"runtime"
"github.com/gorilla/websocket"
"github.com/zserge/webview"
)
func notify(text string, title string, sound string, icon string) {
switch runtime.GOOS {
case "darwin":
notification := fmt.Sprintf("display notification \"%s\" with title \"%s\" sound name \"%s\"", text, title, sound)
cmd := exec.Command("osascript", "-e", notification)
cmd.Start()
case "linux":
cmd := exec.Command("notify-send", "-i", icon, title, text)
cmd.Start()
case "windows":
fmt.Printf("Soon\n")
}
}
type Notification struct {
Title string `json:"title"`
Message string `json:"message"`
}
func handleNotifs() {
url := "ws://localhost:8080/ws"
var dialer *websocket.Dialer
conn, _, err := dialer.Dial(url, nil)
if err != nil {
log.Println(err)
return
}
for {
_, msg, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
var res Notification
json.Unmarshal(msg, &res)
notify(res.Message, res.Title, "", "")
log.Printf("received: %s\n", msg)
}
}
func main() {
fmt.Printf("Start\n")
go handleNotifs()
content, err := ioutil.ReadFile("home.html")
if err != nil {
log.Fatal(err)
}
w := webview.New(webview.Settings{
Title: "NotifyApp",
URL: `data:text/html,` + url.PathEscape(string(content)),
})
w.Run()
}