-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpayload.go
235 lines (198 loc) · 5.93 KB
/
payload.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package apns
import (
"encoding/json"
"errors"
"fmt"
)
//Object describing a push notification payload
type Payload struct {
// Basic alert structure
AlertText string
Badge BadgeNumber
Sound string
ContentAvailable int
Category string
// If this is an enhanced message, use
// an APSAlertBody instead of .Alert
AlertBody APSAlertBody
// Any custom fields to be added to the apns payload
// These exist outside of the `aps` namespace
CustomFields map[string]interface{}
// Payload server fields
// UNIX time in seconds when the payload is invalid
ExpirationTime uint32
// Must be either 5 or 10, if not one of these two values will default to 5
Priority uint8
// Device push token, should contain no spaces
Token string
// Any extra data to be associated with this payload,
// Will not be sent to apple but will be held onto for error cases
ExtraData interface{}
}
type APSAlertBody struct {
// Text of the alert
Body string `json:"body,omitempty"`
// Other alert options
ActionLocKey string `json:"action-loc-key,omitempty"`
LocKey string `json:"loc-key,omitempty"`
LocArgs []string `json:"loc-args,omitempty"`
LaunchImage string `json:"launch-image,omitempty"`
// New Title fields and localizations. >= iOS 8.2
Title string `json:"title,omitempty"`
TitleLocKey string `json:"title-loc-key,omitempty"`
TitleLocArgs []string `json:"title-loc-args,omitempty"`
}
type alertBodyAps struct {
Alert APSAlertBody
Badge BadgeNumber
Sound string
Category string
ContentAvailable int
}
type simpleAps struct {
Alert string
Badge BadgeNumber
Sound string
Category string
ContentAvailable int
}
// Convert a Payload into a json object and then converted to a byte array
// If the number of converted bytes is greater than the maxPayloadSize
// an attempt will be made to truncate the AlertText
// If this cannot be done, then an error will be returned
func (p *Payload) Marshal(maxPayloadSize int) ([]byte, error) {
if p.isSimple() {
return p.marshalSimplePayload(maxPayloadSize)
} else {
return p.marshalAlertBodyPayload(maxPayloadSize)
}
}
//Whether or not to use simple aps format or not
func (p *Payload) isSimple() bool {
return p.AlertBody.Body == ""
}
//Helper method to generate a json compatible map with aps key + custom fields
//will return error if custom field named aps supplied
func constructFullPayload(aps interface{}, customFields map[string]interface{}) (map[string]interface{}, error) {
var fullPayload = make(map[string]interface{})
fullPayload["aps"] = aps
for key, value := range customFields {
if key == "aps" {
return nil, errors.New("Cannot have a custom field named aps")
}
fullPayload[key] = value
}
return fullPayload, nil
}
//Handle simple payload case with just text alert
//Handle truncating of alert text if too long for maxPayloadSize
func (p *Payload) marshalSimplePayload(maxPayloadSize int) ([]byte, error) {
var jsonStr []byte
//use simple payload
aps := simpleAps{
Alert: p.AlertText,
Badge: p.Badge,
Sound: p.Sound,
Category: p.Category,
ContentAvailable: p.ContentAvailable,
}
fullPayload, err := constructFullPayload(aps, p.CustomFields)
if err != nil {
return nil, err
}
jsonStr, err = json.Marshal(fullPayload)
if err != nil {
return nil, err
}
payloadLen := len(jsonStr)
if payloadLen > maxPayloadSize {
clipSize := payloadLen - (maxPayloadSize) + 3 //need extra characters for ellipse
if clipSize > len(p.AlertText) {
return nil, errors.New(fmt.Sprintf("Payload was too long to successfully marshall to less than %v", maxPayloadSize))
}
aps.Alert = aps.Alert[:len(aps.Alert)-clipSize] + "..."
fullPayload["aps"] = aps
if err != nil {
return nil, err
}
jsonStr, err = json.Marshal(fullPayload)
if err != nil {
return nil, err
}
}
return jsonStr, nil
}
//Handle complet payload case with alert object
//Handle truncating of alert text if too long for maxPayloadSize
func (p *Payload) marshalAlertBodyPayload(maxPayloadSize int) ([]byte, error) {
var jsonStr []byte
// Use APSAlertBody payload
aps := alertBodyAps{
Alert: p.AlertBody,
Badge: p.Badge,
Sound: p.Sound,
Category: p.Category,
ContentAvailable: p.ContentAvailable,
}
fullPayload, err := constructFullPayload(aps, p.CustomFields)
if err != nil {
return nil, err
}
jsonStr, err = json.Marshal(fullPayload)
if err != nil {
return nil, err
}
payloadLen := len(jsonStr)
if payloadLen > maxPayloadSize {
clipSize := payloadLen - (maxPayloadSize) + 3 //need extra characters for ellipse
if clipSize > len(p.AlertBody.Body) {
return nil, errors.New(fmt.Sprintf("Payload was too long to successfully marshall %v or less bytes", maxPayloadSize))
}
aps.Alert.Body = aps.Alert.Body[:len(aps.Alert.Body)-clipSize] + "..."
fullPayload["aps"] = aps
if err != nil {
return nil, err
}
jsonStr, err = json.Marshal(fullPayload)
if err != nil {
return nil, err
}
}
return jsonStr, nil
}
func (s simpleAps) MarshalJSON() ([]byte, error) {
toMarshal := make(map[string]interface{})
if s.Alert != "" {
toMarshal["alert"] = s.Alert
}
if s.Badge.IsSet() {
toMarshal["badge"] = s.Badge
}
if s.Sound != "" {
toMarshal["sound"] = s.Sound
}
if s.Category != "" {
toMarshal["category"] = s.Category
}
if s.ContentAvailable != 0 {
toMarshal["content-available"] = s.ContentAvailable
}
return json.Marshal(toMarshal)
}
func (a alertBodyAps) MarshalJSON() ([]byte, error) {
toMarshal := make(map[string]interface{})
toMarshal["alert"] = a.Alert
if a.Badge.IsSet() {
toMarshal["badge"] = a.Badge
}
if a.Sound != "" {
toMarshal["sound"] = a.Sound
}
if a.Category != "" {
toMarshal["category"] = a.Category
}
if a.ContentAvailable != 0 {
toMarshal["content-available"] = a.ContentAvailable
}
return json.Marshal(toMarshal)
}