-
Notifications
You must be signed in to change notification settings - Fork 8
/
toast_windows.go
349 lines (306 loc) · 10.4 KB
/
toast_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//go:build windows
package toast
import (
"bytes"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"sync"
"syscall"
"text/template"
"time"
"unsafe"
)
// WithAppID
//
// The name of your app. This value shows up in Windows 10's Action Centre, so make it
// something readable for your users. It can contain spaces, however special characters
// (eg. é) are not supported.
func WithAppID(appID string) NotificationOption {
return func(n *notification) {
n.AppID = appID
}
}
// WithIcon
//
// An optional path to an image on the OS to display to the left of the title & message.
func WithIcon(pathIcon string) NotificationOption {
return func(n *notification) {
n.Icon = pathIcon
}
}
func WithIconRaw(raw []byte) NotificationOption {
return func(n *notification) {
randBytes := make([]byte, 4)
_r.Read(randBytes)
n._tmpIconFilename = filepath.Join(os.TempDir(), fmt.Sprintf("go-toast-logo-%x.png", randBytes))
if err := os.WriteFile(n._tmpIconFilename, raw, 0600); err != nil {
return
}
n.Icon = n._tmpIconFilename
}
}
// WithActivationType
//
// The type of notification level action (like Action)
func WithActivationType(activationType string) NotificationOption {
return func(n *notification) {
n.ActivationType = activationType
}
}
// WithActivationArguments
//
// // The activation/action arguments (invoked when the user clicks the notification)
func WithActivationArguments(activationArguments string) NotificationOption {
return func(n *notification) {
n.ActivationArguments = activationArguments
}
}
// WithProtocolAction
//
// Defines an actionable button.
// See https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts for more info.
//
// Only protocol type action buttons are actually useful, as there's no way of receiving feedback from the
// user's choice. Examples of protocol type action buttons include: "bingmaps:?q=sushi" to open up Windows 10's
// maps app with a pre-populated search field set to "sushi".
//
// Action{"protocol", "Open Maps", "bingmaps:?q=sushi"}
func WithProtocolAction(label string, arguments ...string) NotificationOption {
return func(n *notification) {
if len(n.Actions) == 0 {
n.Actions = make([]Action, 0, 5)
}
if len(n.Actions) == 5 {
return
}
if len(arguments) == 0 {
arguments = []string{""}
}
n.Actions = append(n.Actions, Action{
Type: "protocol",
Label: escapeNotificationString(label),
Arguments: arguments[0],
})
}
}
// WithAudioLoop
//
// Whether to loop the audio (default false)
func WithAudioLoop(b bool) NotificationOption {
return func(n *notification) {
n.Loop = b
}
}
// WithDuration
//
// How long the notification should show up for (short/long)
func WithDuration(nd NotificationDuration) NotificationOption {
return func(n *notification) {
n.Duration = nd
}
}
func WithLongDuration() NotificationOption {
return func(n *notification) {
n.Duration = Long
}
}
func WithShortDuration() NotificationOption {
return func(n *notification) {
n.Duration = Short
}
}
type NotificationDuration string
const (
Short NotificationDuration = "short"
Long NotificationDuration = "long"
)
const (
Silent Audio = "silent"
Default Audio = "ms-winsoundevent:Notification.Default"
IM Audio = "ms-winsoundevent:Notification.IM"
Mail Audio = "ms-winsoundevent:Notification.Mail"
Reminder Audio = "ms-winsoundevent:Notification.Reminder"
SMS Audio = "ms-winsoundevent:Notification.SMS"
LoopingAlarm Audio = "ms-winsoundevent:Notification.Looping.Alarm"
LoopingAlarm2 Audio = "ms-winsoundevent:Notification.Looping.Alarm2"
LoopingAlarm3 Audio = "ms-winsoundevent:Notification.Looping.Alarm3"
LoopingAlarm4 Audio = "ms-winsoundevent:Notification.Looping.Alarm4"
LoopingAlarm5 Audio = "ms-winsoundevent:Notification.Looping.Alarm5"
LoopingAlarm6 Audio = "ms-winsoundevent:Notification.Looping.Alarm6"
LoopingAlarm7 Audio = "ms-winsoundevent:Notification.Looping.Alarm7"
LoopingAlarm8 Audio = "ms-winsoundevent:Notification.Looping.Alarm8"
LoopingAlarm9 Audio = "ms-winsoundevent:Notification.Looping.Alarm9"
LoopingAlarm10 Audio = "ms-winsoundevent:Notification.Looping.Alarm10"
LoopingCall Audio = "ms-winsoundevent:Notification.Looping.Call"
LoopingCall2 Audio = "ms-winsoundevent:Notification.Looping.Call2"
LoopingCall3 Audio = "ms-winsoundevent:Notification.Looping.Call3"
LoopingCall4 Audio = "ms-winsoundevent:Notification.Looping.Call4"
LoopingCall5 Audio = "ms-winsoundevent:Notification.Looping.Call5"
LoopingCall6 Audio = "ms-winsoundevent:Notification.Looping.Call6"
LoopingCall7 Audio = "ms-winsoundevent:Notification.Looping.Call7"
LoopingCall8 Audio = "ms-winsoundevent:Notification.Looping.Call8"
LoopingCall9 Audio = "ms-winsoundevent:Notification.Looping.Call9"
LoopingCall10 Audio = "ms-winsoundevent:Notification.Looping.Call10"
)
// Action
//
// Defines an actionable button.
// See https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts for more info.
//
// Only protocol type action buttons are actually useful, as there's no way of receiving feedback from the
// user's choice. Examples of protocol type action buttons include: "bingmaps:?q=sushi" to open up Windows 10's
// maps app with a pre-populated search field set to "sushi".
//
// Action{"protocol", "Open Maps", "bingmaps:?q=sushi"}
type Action struct {
Type string
Label string
Arguments string
}
var _ notifier = (*notification)(nil)
func newNotification(message string, opts ...NotificationOption) *notification {
n := ¬ification{
AppID: "GO APP",
Title: "GO APP",
Message: message,
ActivationType: "protocol",
Duration: Short,
Audio: Silent,
}
for _, fn := range opts {
fn(n)
}
n.AppID = escapeNotificationString(n.AppID)
n.Title = escapeNotificationString(n.Title)
n.Message = escapeNotificationString(n.Message)
return n
}
func (n *notification) push() error {
content, err := n.template()
if err != nil {
return err
}
randBytes := make([]byte, 4)
_r.Read(randBytes)
tmpFilename := filepath.Join(os.TempDir(), fmt.Sprintf("go-toast-%x.ps1", randBytes))
if err = os.WriteFile(tmpFilename, content, 0600); err != nil {
return err
}
defer func() {
_ = os.Remove(tmpFilename)
}()
launch := "(Get-Content -Encoding UTF8 -Path " + tmpFilename + " -Raw) | Invoke-Expression"
if len(n._tmpIconFilename) != 0 {
launch += "; Start-Sleep -m 50 ; Remove-Item " + n._tmpIconFilename
}
cmd := exec.Command("PowerShell", "-ExecutionPolicy", "Bypass", launch)
fixCmd("PowerShell", cmd)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return cmd.Run()
}
var (
_r = rand.New(rand.NewSource(time.Now().Unix()))
_tpl *template.Template
_once sync.Once
)
func (n *notification) template() (content []byte, err error) {
_once.Do(func() {
var tplNotification = `
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$APP_ID = '{{if .AppID}}{{.AppID}}{{else}}Windows App{{end}}'
$template = @"
<toast activationType="{{.ActivationType}}" launch="{{.ActivationArguments}}" duration="{{.Duration}}">
<visual>
<binding template="ToastGeneric">
{{if .Icon}}
<image placement="appLogoOverride" src="{{.Icon}}" />
{{end}}
{{if .Title}}
<text><![CDATA[{{.Title}}]]></text>
{{end}}
{{if .Message}}
<text><![CDATA[{{.Message}}]]></text>
{{end}}
</binding>
</visual>
{{if ne .Audio "silent"}}
<audio src="{{.Audio}}" loop="{{.Loop}}" />
{{else}}
<audio silent="true" />
{{end}}
{{if .Actions}}
<actions>
{{range .Actions}}
<action activationType="{{.Type}}" content="{{.Label}}" arguments="{{.Arguments}}" />
{{end}}
</actions>
{{end}}
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$go_toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($go_toast)
`
_tpl, err = template.New("_tpl").Parse(tplNotification)
})
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
err = _tpl.Execute(buf, n)
return buf.Bytes(), err
}
type notification struct {
// The name of your app. This value shows up in Windows 10's Action Centre, so make it
// something readable for your users. It can contain spaces, however special characters
// (eg. é) are not supported.
AppID string
// The main title/heading for the notification.
Title string
// The single/multi line message to display for the notification.
Message string
// An optional path to an image on the OS to display to the left of the title & message.
Icon string
_tmpIconFilename string
// The type of notification level action (like Action)
ActivationType string
// The activation/action arguments (invoked when the user clicks the notification)
ActivationArguments string
// Optional action buttons to display below the notification title & message.
Actions []Action
// The audio to play when displaying the notification
Audio Audio
// Whether to loop the audio (default false)
Loop bool
// How long the notification should show up for (short/long)
Duration NotificationDuration
}
func escapeNotificationString(in string) string {
noSlash := strings.ReplaceAll(in, "`", "``")
return strings.ReplaceAll(noSlash, "\"", "`\"")
}
// https://pkg.go.dev/golang.org/x/sys/execabs#Command
func fixCmd(name string, cmd *exec.Cmd) {
if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) {
// exec.Command was called with a bare binary name and
// exec.LookPath returned a path which is not absolute.
// Set cmd.lookPathErr and clear cmd.Path so that it
// cannot be run.
lookPathErr := (*error)(unsafe.Pointer(reflect.ValueOf(cmd).Elem().FieldByName("lookPathErr").Addr().Pointer()))
if *lookPathErr == nil {
*lookPathErr = relError(name, cmd.Path)
}
cmd.Path = ""
}
}
func relError(file, path string) error {
return fmt.Errorf("%s resolves to executable in current directory (.%c%s)", file, filepath.Separator, path)
}