/
notification.go
108 lines (94 loc) · 2.66 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
102
103
104
105
106
107
108
package fcm
import gofcm "github.com/NaySoftware/go-fcm"
type Notification struct {
Payload map[string]string `json:"-"`
CollapseKey *string `json:"-"`
TimeToLive int `json:"-"`
DelayWhileIdle bool `json:"-"`
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Icon string `json:"icon,omitempty"`
Sound string `json:"sound,omitempty"`
Badge string `json:"badge,omitempty"`
Tag string `json:"tag,omitempty"`
Color string `json:"color,omitempty"`
ClickAction string `json:"click_action,omitempty"`
BodyLocKey string `json:"body_loc_key,omitempty"`
BodyLocArgs string `json:"body_loc_args,omitempty"`
TitleLocKey string `json:"title_loc_key,omitempty"`
TitleLocArgs string `json:"title_loc_args,omitempty"`
AndroidChannelID string `json:"android_channel_id,omitempty"`
}
func NewNotification(title string, opts ...Opt) *Notification {
n := &Notification{Title: title}
for _, o := range opts {
o(n)
}
return n
}
func WithBody(body string) Opt {
return func(n *Notification) {
n.Body = body
}
}
func WithIcon(icon string) Opt {
return func(n *Notification) {
n.Icon = icon
}
}
func WithColor(color string) Opt {
return func(n *Notification) {
n.Color = color
}
}
func WithSound(sound string) Opt {
return func(n *Notification) {
n.Sound = sound
}
}
func WithBadge(badge string) Opt {
return func(n *Notification) {
n.Badge = badge
}
}
func WithAction(action string) Opt {
return func(n *Notification) {
n.ClickAction = action
}
}
func WithTag(tag string) Opt {
return func(n *Notification) {
n.Tag = tag
}
}
func WithPayload(payload map[string]string) Opt {
return func(n *Notification) {
n.Payload = payload
}
}
func WithCollapseKey(collapseKey string) Opt {
return func(n *Notification) {
if len(collapseKey) < 1 {
return
}
n.CollapseKey = &collapseKey
}
}
func (s *Notification) toNotificationPayload() *gofcm.NotificationPayload {
return &gofcm.NotificationPayload{
Title: s.Title,
Body: s.Body,
TitleLocKey: s.TitleLocKey,
TitleLocArgs: s.TitleLocArgs,
Tag: s.Tag,
Sound: s.Sound,
Icon: s.Icon,
Color: s.Color,
ClickAction: s.ClickAction,
BodyLocKey: s.BodyLocKey,
BodyLocArgs: s.BodyLocArgs,
Badge: s.Badge,
AndroidChannelID: s.AndroidChannelID,
}
}
type Opt func(*Notification)