-
Notifications
You must be signed in to change notification settings - Fork 4
/
message_web.go
133 lines (101 loc) · 3.65 KB
/
message_web.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
package hms
import (
"errors"
"time"
)
type WebPushConfig struct {
// WebPush message header
Headers *WebPushHeaders `json:"headers,omitempty"`
// WebPush notification message structure
Notification *WebPushNotification `json:"notification,omitempty"`
// WebPush agent parameter
HmsOptions *HmsWebPushOption `json:"hms_options,omitempty"`
}
type WebPushHeaders struct {
// Message cache time, in seconds, for example, 20, 20s, or 20S.
TTL *TTL `json:"ttl,omitempty"`
// Message ID, which can be used to overwrite undelivered messages.
Topic string `json:"topics,omitempty"`
// Message emergency level. The value can only be very-low, low, normal, or high.
Urgency Urgency `json:"urgency,omitempty"`
}
type HmsWebPushOption struct {
// Default URL for redirection when no action is performed.
Link string `json:"link,omitempty"`
}
type WebPushNotification struct {
// Title of a web app notification message.
// If the title parameter is set, the value of the message.notification.title field is overwritten.
// Before a message is sent, you must set at least one of title and message.notification.title.
Title string `json:"title,omitempty"`
// Body of a web app notification message.
// If the body parameter is set, the value of the message.notification.body field is overwritten.
// Before a message is sent, you must set at least one of body and message.notification.body.
Body string `json:"body,omitempty"`
// Small icon URL.
Icon string `json:"icon,omitempty"`
// Large image URL.
Image string `json:"image,omitempty"`
// Language.
Lang string `json:"lang,omitempty"`
// Notification message group tag. Multiple same tags are collapsed and the latest one is displayed.
// This function is used only for mobile phone browsers.
Tag string `json:"tag,omitempty"`
// Browser icon URL, which only applies to mobile phone browsers and is used to replace the default browser icon.
Badge string `json:"badge,omitempty"`
// Text direction, which can be set to auto, ltr, or rtl.
Dir TextDirection `json:"dir,omitempty"`
// Vibration interval, in milliseconds. The value is an integer by default. The value range is [100,200,300].
Vibrate []int `json:"vibrate,omitempty"`
// Message reminding flag.
Renotify bool `json:"renotify,omitempty"`
// Indicates that notification messages should remain active until a user taps or closes them.
RequireInteraction bool `json:"require_interaction,omitempty"`
// Message sound-free and vibration-free reminding flag.
Silent bool `json:"silent,omitempty"`
// Sending timestamp.
Timestamp int64 `json:"timestamp,omitempty"`
// Message action.
Actions []*WebPushAction `json:"actions,omitempty"`
}
type WebPushAction struct {
// Action name.
Action string `json:"action,omitempty"`
// URL for the button icon of an action.
Icon string `json:"icon,omitempty"`
// Title of an action.
Title string `json:"title,omitempty"`
}
func GetDefaultWebNotification() *WebPushNotification {
return &WebPushNotification{
Dir: TextDirAuto,
Silent: true,
Timestamp: time.Now().Unix(),
}
}
func validateWebPushConfig(webPushConfig *WebPushConfig) error {
if webPushConfig == nil {
return nil
}
return validateWebPushNotification(webPushConfig.Notification)
}
func validateWebPushNotification(notification *WebPushNotification) error {
if notification == nil {
return nil
}
if err := validateWebPushAction(notification.Actions); err != nil {
return err
}
return nil
}
func validateWebPushAction(actions []*WebPushAction) error {
if actions == nil {
return nil
}
for _, action := range actions {
if action.Action == "" {
return errors.New("web common action can't be empty")
}
}
return nil
}