/
client.go
142 lines (114 loc) · 3.23 KB
/
client.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
package fcm
import (
"context"
gofcm "github.com/NaySoftware/go-fcm"
"strconv"
)
func (s *fcmClient) ShowPrintResult(show bool) {
s.showPrintResult = show
}
// Send notification to a topic
func (s *fcmClient) SendToTopic(ctx context.Context, topic string, notification *Notification) (*Response, error) {
s.client.NewFcmTopicMsg(topic, notification.Payload)
s.prepare(notification)
status, err := s.send()
if err != nil {
return nil, err
}
response := s.parseResponse(status)
return response, nil
}
// Send notification to single device
func (s *fcmClient) SendToDevice(ctx context.Context, deviceId string, notification *Notification) (*Response, error) {
if notification == nil {
return nil, ErrNotificationEmpty
}
s.client.NewFcmMsgTo(deviceId, notification.Payload)
if ck := notification.CollapseKey; ck != nil {
s.client.SetCollapseKey(*ck)
}
s.client.SetNotificationPayload(notification.toNotificationPayload())
s.prepare(notification)
status, err := s.send()
if err != nil {
return nil, err
}
response := s.parseResponse(status)
return response, nil
}
// Send notification to multi-devices
func (s *fcmClient) SendToDevices(ctx context.Context, deviceIds []string, notification *Notification) (*Response, error) {
if notification == nil {
return nil, ErrNotificationEmpty
}
s.client.NewFcmRegIdsMsg(deviceIds, notification.Payload)
s.client.SetNotificationPayload(notification.toNotificationPayload())
if ck := notification.CollapseKey; ck != nil {
s.client.SetCollapseKey(*ck)
}
s.prepare(notification)
status, err := s.send()
if err != nil {
return nil, err
}
response := s.parseResponse(status)
return response, nil
}
// Parse from NaySoftware response status -> fcm response
func (s *fcmClient) parseResponse(status *gofcm.FcmResponseStatus) *Response {
response := &Response{
Success: status.Success,
Fail: status.Fail,
Results: make([]Result, len(status.Results)),
}
// case send TOPIC successfully
if status.MsgId > 0 {
response.Results = make([]Result, 1)
msgId := strconv.FormatInt(status.MsgId, 10)
response.Results[0] = Result{
MessageID: &msgId,
DeviceToken: s.client.Message.To,
}
return response
}
for index, item := range status.Results {
messageId := item["message_id"]
err := item["error"]
deviceToken := s.client.Message.To
if len(deviceToken) < 1 && len(s.client.Message.RegistrationIds) > 0 {
deviceToken = s.client.Message.RegistrationIds[index]
}
if len(messageId) > 0 {
response.Results[index] = Result{
MessageID: &messageId,
DeviceToken: deviceToken,
}
}
if len(err) > 0 {
response.Results[index] = Result{
DeviceToken: deviceToken,
Error: createCustomError(err),
}
}
}
return response
}
// Prepare before sending message
func (s *fcmClient) prepare(notification *Notification) {
if notification.CollapseKey != nil {
s.client.SetCollapseKey(*notification.CollapseKey)
}
s.client.SetTimeToLive(notification.TimeToLive)
s.client.SetDelayWhileIdle(notification.DelayWhileIdle)
}
// Send notification
func (s *fcmClient) send() (*gofcm.FcmResponseStatus, error) {
status, err := s.client.Send()
if err != nil {
return nil, err
}
if s.showPrintResult {
status.PrintResults()
}
return status, nil
}