-
Notifications
You must be signed in to change notification settings - Fork 92
/
notify_windows.go
127 lines (105 loc) · 2.6 KB
/
notify_windows.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//go:build windows && !linux && !freebsd && !netbsd && !openbsd && !darwin && !js
// +build windows,!linux,!freebsd,!netbsd,!openbsd,!darwin,!js
package beeep
import (
"bufio"
"bytes"
"errors"
"os/exec"
"strings"
"syscall"
"time"
toast "github.com/go-toast/toast"
"github.com/tadvi/systray"
"golang.org/x/sys/windows/registry"
)
var isWindows10 bool
var applicationID string
func init() {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
if err != nil {
return
}
defer k.Close()
maj, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
if err != nil {
return
}
isWindows10 = maj == 10
if isWindows10 {
applicationID = appID()
}
}
// Notify sends desktop notification.
func Notify(title, message, appIcon string) error {
if isWindows10 {
return toastNotify(title, message, appIcon)
}
err := baloonNotify(title, message, appIcon, false)
if err != nil {
e := msgNotify(title, message)
if e != nil {
return errors.New("beeep: " + err.Error() + "; " + e.Error())
}
}
return nil
}
func msgNotify(title, message string) error {
msg, err := exec.LookPath("msg")
if err != nil {
return err
}
cmd := exec.Command(msg, "*", "/TIME:3", title+"\n\n"+message)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return cmd.Run()
}
func baloonNotify(title, message, appIcon string, bigIcon bool) error {
tray, err := systray.New()
if err != nil {
return err
}
err = tray.ShowCustom(pathAbs(appIcon), title)
if err != nil {
return err
}
go func() {
go func() {
_ = tray.Run()
}()
time.Sleep(3 * time.Second)
_ = tray.Stop()
}()
return tray.ShowMessage(title, message, bigIcon)
}
func toastNotify(title, message, appIcon string) error {
notification := toastNotification(title, message, pathAbs(appIcon))
return notification.Push()
}
func toastNotification(title, message, appIcon string) toast.Notification {
return toast.Notification{
AppID: applicationID,
Title: title,
Message: message,
Icon: appIcon,
}
}
func appID() string {
defID := "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe"
cmd := exec.Command("powershell", "-NoProfile", "Get-StartApps")
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := cmd.Output()
if err != nil {
return defID
}
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.Contains(line, "powershell.exe") {
sp := strings.Split(line, " ")
if len(sp) > 0 {
return sp[len(sp)-1]
}
}
}
return defID
}