This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
100 lines (84 loc) · 2.95 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gcmlib
import (
"errors"
"fmt"
"strings"
)
const (
maxRegistrationIDs = 1000
maxTTL = 4 * 7 * 24 * 60 * 60
maxPriority = 10
)
type Message struct {
// Targets
To string `json:"to,omitempty"`
RegistrationIDs []string `json:"registration_ids"`
// Options
CollapseKey string `json:"collapse_key,omitempty"`
Priority uint8 `json:"priority,omitempty"`
ContentAvailable bool `json:"content_available,omitempty"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TTL uint `json:"time_to_live,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
// Payload
Notification *Notification `json:"notification,omitempty"`
Data map[string]string `json:"data,omitempty"`
}
type Notification struct {
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"`
TitleLocArgs string `json:"title_loc_args,omitempty"`
TitleLocKey string `json:"title_loc_key,omitempty"`
}
var reservedDataKeys = []string{"from", "collapse_key"}
var (
errNoRegID = errors.New("requires at least one registration id")
errBothToAndRegID = errors.New("cannot set both 'to' and 'registration_ids' field")
errInvalidPriority = errors.New("priority value must be between 0 - 10")
errExceedMaxRegIDs = fmt.Errorf("registration_ids field cannot contain more than %d registration id", maxRegistrationIDs)
errInvalidTTL = fmt.Errorf("time_to_live value must be between 0 - %d", maxTTL)
errReservedDataKey = fmt.Errorf("data must not contain a reserved key. Reserved keys: %s", strings.Join(reservedDataKeys, ", "))
errReservedDataKeyPrefix = errors.New("data must not contain keys beginning with gcm or google")
)
func (m *Message) Validate() error {
if (m.To == "") && (len(m.RegistrationIDs) == 0) {
return errNoRegID
}
if (m.To != "") && (len(m.RegistrationIDs) > 0) {
return errBothToAndRegID
}
// check len(RegistrationIDs) > 1000
if len(m.RegistrationIDs) > maxRegistrationIDs {
return errExceedMaxRegIDs
}
// check TTL
if m.TTL > maxTTL {
return errInvalidTTL
}
// check priority
if m.Priority > maxPriority {
return errInvalidPriority
}
// check data keys...
for key := range m.Data {
for _, rkey := range reservedDataKeys {
if key == rkey {
return errReservedDataKey
}
}
if strings.HasPrefix(key, "google") || strings.HasPrefix(key, "gcm") {
return errReservedDataKeyPrefix
}
}
//
return nil
}