forked from Iwark/pushnotification
-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
59 lines (53 loc) · 1.22 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
package pushnotification
import "encoding/json"
type message struct {
APNS string `json:"APNS"`
APNSSandbox string `json:"APNS_SANDBOX"`
Default string `json:"default"`
GCM string `json:"GCM"`
}
func newMessageJSON(data *Data) (m string, err error) {
b, err := json.Marshal(map[string]interface{}{
"aps": enrich(map[string]interface{}{
"alert": map[string]interface{}{
"body": data.Alert,
"title": data.Subject,
},
"sound": data.Sound,
"badge": data.Badge,
"content-available": 1,
}, data.Data),
})
if err != nil {
return
}
payload := string(b)
b, err = json.Marshal(map[string]interface{}{
"data": enrich(map[string]interface{}{
"message": data.Alert,
"badge": data.Badge,
"title": data.Subject,
}, data.Data),
})
if err != nil {
return
}
gcm := string(b)
pushData, err := json.Marshal(message{
Default: *data.Alert,
APNS: payload,
APNSSandbox: payload,
GCM: gcm,
})
if err != nil {
return
}
m = string(pushData)
return
}
func enrich(message map[string]interface{}, custom map[string]interface{}) map[string]interface{} {
for key, value := range custom {
message[key] = value
}
return message
}