forked from mattias-lundell/go-fcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcm_firbase_admin.go
132 lines (106 loc) · 2.68 KB
/
fcm_firbase_admin.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
package fcm
import (
"encoding/json"
"net/http"
"strconv"
messaging "firebase.google.com/go/v4/messaging"
)
func (this *FcmMsg) makeMulticastMessageData() (*map[string]string, bool) {
if this.Data == nil {
emptyMap := make(map[string]string)
return &emptyMap, true
}
data, ok := this.Data.(map[string]interface{})
dataMap := make(map[string]string)
if !ok {
return nil, false
}
titleField, ok := data["title"]
if ok {
dataMap["title"] = titleField.(string)
}
bodyField, ok := data["body"]
if ok {
dataMap["body"] = bodyField.(string)
}
itemTypeField, ok := data["item_type"]
if ok {
dataMap["item_type"] = itemTypeField.(string)
}
itemIdField, ok := data["item_id"]
if ok {
dataMap["item_id"] = itemIdField.(string)
}
deepLinkField, ok := data["deeplink"]
if ok {
dataMap["deeplink"] = deepLinkField.(string)
}
imageUrlField, ok := data["image_url"]
if ok {
dataMap["image_url"] = imageUrlField.(string)
}
soundField, ok := data["sound"]
if ok {
dataMap["sound"] = soundField.(string)
}
actorNicknameField, ok := data["actor_nickname"]
if ok {
dataMap["actor_nickname"] = actorNicknameField.(string)
}
badgeCountField, ok := data["badge_count"]
if ok {
dataMap["badge_count"] = strconv.Itoa(int(badgeCountField.(float64)))
}
actionsField, ok := data["actions"]
if ok {
bytes, err := json.Marshal(actionsField)
if err == nil {
dataMap["actions"] = string(bytes)
}
}
trackingPayloadField, ok := data["tracking_payload"]
if ok {
bytes, err := json.Marshal(trackingPayloadField)
if err == nil {
dataMap["tracking_payload"] = string(bytes)
}
}
return &dataMap, true
}
func toFcmRespStatus(resp *messaging.BatchResponse) *FcmResponseStatus {
var ok bool
var statusCode int = http.StatusInternalServerError
if resp.SuccessCount > 0 {
ok = true
statusCode = http.StatusOK
}
if resp.FailureCount > 0 {
// NOTE: With Ok set to false bonito will try to inspect responses and handle errors.
ok = false
}
status := FcmResponseStatus{
Ok: ok,
StatusCode: statusCode,
Success: resp.SuccessCount,
Fail: resp.FailureCount,
Canonical_ids: 0, // TODO Where does it come from? Is it needed?
Results: *toFcmResponseResults(&resp.Responses),
}
return &status
}
func toFcmResponseResults(original *[]*messaging.SendResponse) *[]map[string]string {
var result []map[string]string
var elem map[string]string
for _, resp := range *original {
elem = map[string]string{
"success": strconv.FormatBool(resp.Success),
"messageID": resp.MessageID,
}
optErr := resp.Error
if optErr != nil {
elem["error"] = optErr.Error()
}
result = append(result, elem)
}
return &result
}