forked from dolfly/termux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.go
101 lines (86 loc) · 2.31 KB
/
notification.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
package termux
import "bytes"
// NotificationPriority enumerates the priority level of the notification
type NotificationPriority string
const (
// Max notification priority
Max NotificationPriority = "max"
// High notification priority
High NotificationPriority = "high"
// Default notification priority
Default NotificationPriority = "default"
// Low notification priority
Low NotificationPriority = "low"
// Min notification priority
Min NotificationPriority = "min"
)
// NotificationButton represents a button shown in the notification bar
type NotificationButton struct {
Text string
Action string
}
// NotificationOpt represents the options of a notification
type NotificationOpt struct {
Content string
Sound bool
Title string
Vibrate []int32
Priority NotificationPriority
LED struct {
Color string
On int
Off int
}
Action string
DeleteAction string
Btn1 NotificationButton
Btn2 NotificationButton
Btn3 NotificationButton
}
// NotificationRemove removes the notification with the given id
func NotificationRemove(id string) error {
buf := bytes.NewBuffer([]byte{})
exec(nil, buf, "NotificationRemove", map[string]interface{}{
"id": id,
}, "")
return checkErr(buf.Bytes())
}
// Notification creates a new notification with the given id and options
func Notification(id string, opt NotificationOpt) error {
in := bytes.NewBuffer([]byte{})
out := bytes.NewBuffer([]byte{})
in.WriteString(opt.Content)
param := map[string]interface{}{
"priority": opt.Priority,
"title": opt.Title,
"sound": opt.Sound,
"id": id,
}
if opt.LED.On > 0 && opt.LED.Off > 0 {
param["led-on"] = opt.LED.On
param["led-off"] = opt.LED.Off
}
if opt.Vibrate != nil {
param["vibrate"] = opt.Vibrate
}
for key, val := range map[string]string{
"led-color": opt.LED.Color,
"action": opt.Action,
"on_delete_action": opt.DeleteAction,
"button_text_1": opt.Btn1.Text,
"button_action_1": opt.Btn1.Action,
"button_text_2": opt.Btn2.Text,
"button_action_2": opt.Btn2.Action,
"button_text_3": opt.Btn3.Text,
"button_action_3": opt.Btn3.Action,
} {
if val == "" {
continue
}
param[key] = val
}
if err := exec(in, out, "Notification", param, ""); err != nil {
return err
}
return checkErr(out.Bytes())
}