-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
executable file
·79 lines (68 loc) · 3.14 KB
/
message.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
package fcm
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")
)
// Notification specifies the predefined, user-visible key-value pairs of the
// notification payload.
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"`
}
// Message represents list of targets, options, and payload for HTTP JSON
// messages.
type Message struct {
To string `json:"to,omitempty"`
RegistrationIDs []string `json:"registration_ids,omitempty"`
Condition string `json:"condition,omitempty"`
CollapseKey string `json:"collapse_key,omitempty"`
Priority string `json:"priority,omitempty"`
ContentAvailable bool `json:"content_available,omitempty"`
MutableContent bool `json:"mutable_content,omitempty"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TimeToLive *uint `json:"time_to_live,omitempty"`
DeliveryReceiptRequested bool `json:"delivery_receipt_requested,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
Notification *Notification `json:"notification,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}
// 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.Condition, "&&") + strings.Count(msg.Condition, "||")
if msg.To == "" && (msg.Condition == "" || opCnt > 2) && len(msg.RegistrationIDs) == 0 {
return ErrInvalidTarget
}
if len(msg.RegistrationIDs) > 1000 {
return ErrToManyRegIDs
}
if msg.TimeToLive != nil && *msg.TimeToLive > uint(2419200) {
return ErrInvalidTimeToLive
}
return nil
}