vendor/github.com/gen2brain/beeep/notify_js.go
51 lines · 967 bytes
1//go:build js
2// +build js
3
4package beeep
5
6import (
7 "syscall/js"
8)
9
10// Notify sends desktop notification.
11//
12// On Web, in Firefox it just works, in Chrome you must call it from some "user gesture" like `onclick`,
13// and you must use TLS certificate, it doesn't work with plain http.
14func Notify(title, message, appIcon string) (err error) {
15 defer func() {
16 e := recover()
17
18 if e == nil {
19 return
20 }
21
22 if e, ok := e.(*js.Error); ok {
23 err = e
24 } else {
25 panic(e)
26 }
27 }()
28
29 n := js.Global().Get("Notification")
30
31 opts := js.Global().Get("Object").Invoke()
32 opts.Set("body", message)
33 opts.Set("icon", pathAbs(appIcon))
34
35 if n.Get("permission").String() == "granted" {
36 n.New(js.ValueOf(title), opts)
37 } else {
38 var f js.Func
39 f = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
40 if args[0].String() == "granted" {
41 n.New(js.ValueOf(title), opts)
42 }
43 f.Release()
44 return nil
45 })
46
47 n.Call("requestPermission", f)
48 }
49
50 return
51}