-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
98 lines (80 loc) · 2.86 KB
/
message.go
File metadata and controls
98 lines (80 loc) · 2.86 KB
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
package hcm
import (
"errors"
"strings"
)
var (
// ErrInvalidMessage occurs if push notitication message is nil.
ErrInvalidMessage = errors.New("message is invalid")
// ErrInvalidTarget occurs if message topic is empty.
ErrInvalidTarget = errors.New("topic is invalid or registration ids are not set")
// ErrToManyRegIDs occurs when registration ids more then 1000.
ErrToManyRegIDs = errors.New("too many registrations ids")
// ErrInvalidTimeToLive occurs if TimeToLive more then 2419200.
ErrInvalidTimeToLive = errors.New("messages time-to-live is invalid")
)
type notification struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
ChannelID string `json:"android_channel_id,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"`
}
type message struct {
Token []string `json:"token,omitempty"`
Data string `json:"data,omitempty"`
Notification *notification `json:"notification,omitempty"`
Android *androidConfig `json:"android,omitempty"`
Topic string `json:"topic,omitempty"`
Condition string `json:"condition,omitempty"`
}
type androidConfig struct {
CollapseKey string `json:"collapse_key,omitempty"`
TimeToLive string `json:"ttl,omitempty"`
Notification *notification `json:"notification,omitempty"`
}
type Message struct {
ValidateOnly bool `json:"validate_only"`
Message *message `json:"message"`
extra map[string]interface{}
}
func NewMessage(token []string, data string, ttl string, isProd bool, extra map[string]interface{}) *Message {
msg := message{
Token: token,
Data: data,
Android: &androidConfig{TimeToLive: ttl}}
return &Message{
ValidateOnly: !isProd,
Message: &msg,
extra: extra,
}
}
func (msg *Message) SetExtra(extra map[string]interface{}) {
msg.extra = extra
}
func (msg *Message) Extra() map[string]interface{} {
return msg.extra
}
// Validate returns an error if the message is not well-formed.
func (msg *Message) Validate() error {
if msg == nil {
return ErrInvalidMessage
}
// validate target identifier: `to` or `condition`, or `registration_ids`
opCnt := strings.Count(msg.Message.Condition, "&&") + strings.Count(msg.Message.Condition, "||")
if (msg.Message.Condition == "" || opCnt > 2) && len(msg.Message.Token) == 0 {
return ErrInvalidTarget
}
if len(msg.Message.Token) > 1000 {
return ErrToManyRegIDs
}
return nil
}