-
Notifications
You must be signed in to change notification settings - Fork 94
/
notify_js.go
51 lines (41 loc) · 967 Bytes
/
notify_js.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
//go:build js
// +build js
package beeep
import (
"syscall/js"
)
// Notify sends desktop notification.
//
// On Web, in Firefox it just works, in Chrome you must call it from some "user gesture" like `onclick`,
// and you must use TLS certificate, it doesn't work with plain http.
func Notify(title, message, appIcon string) (err error) {
defer func() {
e := recover()
if e == nil {
return
}
if e, ok := e.(*js.Error); ok {
err = e
} else {
panic(e)
}
}()
n := js.Global().Get("Notification")
opts := js.Global().Get("Object").Invoke()
opts.Set("body", message)
opts.Set("icon", pathAbs(appIcon))
if n.Get("permission").String() == "granted" {
n.New(js.ValueOf(title), opts)
} else {
var f js.Func
f = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if args[0].String() == "granted" {
n.New(js.ValueOf(title), opts)
}
f.Release()
return nil
})
n.Call("requestPermission", f)
}
return
}