/
notification_hms.go
223 lines (176 loc) · 4.86 KB
/
notification_hms.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
package gorush
import (
"context"
"encoding/json"
"errors"
"sync"
"github.com/msalihkarakasli/go-hms-push/push/config"
"github.com/msalihkarakasli/go-hms-push/push/core"
"github.com/msalihkarakasli/go-hms-push/push/model"
)
var (
pushError error
pushClient *core.HMSClient
once sync.Once
)
// GetPushClient use for create HMS Push
func GetPushClient(conf *config.Config) (*core.HMSClient, error) {
once.Do(func() {
client, err := core.NewHttpClient(conf)
if err != nil {
panic(err)
}
pushClient = client
pushError = err
})
return pushClient, pushError
}
// InitHMSClient use for initialize HMS Client.
func InitHMSClient(appSecret, appID string) (*core.HMSClient, error) {
if appSecret == "" {
return nil, errors.New("Missing Huawei App Secret")
}
if appID == "" {
return nil, errors.New("Missing Huawei App ID")
}
conf := &config.Config{
AppId: appID,
AppSecret: appSecret,
AuthUrl: "https://oauth-login.cloud.huawei.com/oauth2/v3/token",
PushUrl: "https://push-api.cloud.huawei.com",
}
if appSecret != PushConf.Huawei.AppSecret || appID != PushConf.Huawei.AppID {
return GetPushClient(conf)
}
if HMSClient == nil {
return GetPushClient(conf)
}
return HMSClient, nil
}
// GetHuaweiNotification use for define HMS notification.
// HTTP Connection Server Reference for HMS
// https://developer.huawei.com/consumer/en/doc/development/HMS-References/push-sendapi
func GetHuaweiNotification(req PushNotification) (*model.MessageRequest, error) {
msgRequest := model.NewNotificationMsgRequest()
msgRequest.Message.Android = model.GetDefaultAndroid()
if len(req.Tokens) > 0 {
msgRequest.Message.Token = req.Tokens
}
if len(req.Topic) > 0 {
msgRequest.Message.Topic = req.Topic
}
if len(req.To) > 0 {
msgRequest.Message.Topic = req.To
}
if len(req.Condition) > 0 {
msgRequest.Message.Condition = req.Condition
}
if req.Priority == "high" {
msgRequest.Message.Android.Urgency = "HIGH"
}
// if req.HuaweiCollapseKey != nil {
msgRequest.Message.Android.CollapseKey = req.HuaweiCollapseKey
//}
if len(req.Category) > 0 {
msgRequest.Message.Android.Category = req.Category
}
if len(req.HuaweiTTL) > 0 {
msgRequest.Message.Android.TTL = req.HuaweiTTL
}
if len(req.BiTag) > 0 {
msgRequest.Message.Android.BiTag = req.BiTag
}
msgRequest.Message.Android.FastAppTarget = req.FastAppTarget
// Add data fields
if len(req.HuaweiData) > 0 {
msgRequest.Message.Data = req.HuaweiData
} else {
// Notification Message
msgRequest.Message.Android.Notification = model.GetDefaultAndroidNotification()
n := msgRequest.Message.Android.Notification
isNotificationSet := false
if req.HuaweiNotification != nil {
isNotificationSet = true
n = req.HuaweiNotification
if n.ClickAction == nil {
n.ClickAction = model.GetDefaultClickAction()
}
}
if len(req.Message) > 0 {
isNotificationSet = true
n.Body = req.Message
}
if len(req.Title) > 0 {
isNotificationSet = true
n.Title = req.Title
}
if len(req.Image) > 0 {
isNotificationSet = true
n.Image = req.Image
}
if v, ok := req.Sound.(string); ok && len(v) > 0 {
isNotificationSet = true
n.Sound = v
} else {
n.DefaultSound = true
}
if isNotificationSet {
msgRequest.Message.Android.Notification = n
}
}
b, err := json.Marshal(msgRequest)
if err != nil {
LogError.Error("Failed to marshal the default message! Error is " + err.Error())
return nil, err
}
LogAccess.Debugf("Default message is %s", string(b))
return msgRequest, nil
}
// PushToHuawei provide send notification to Android server.
func PushToHuawei(req PushNotification) bool {
LogAccess.Debug("Start push notification for Huawei")
var (
client *core.HMSClient
retryCount = 0
maxRetry = PushConf.Huawei.MaxRetry
)
if req.Retry > 0 && req.Retry < maxRetry {
maxRetry = req.Retry
}
// check message
err := CheckMessage(req)
if err != nil {
LogError.Error("request error: " + err.Error())
return false
}
Retry:
isError := false
notification, _ := GetHuaweiNotification(req)
client, err = InitHMSClient(PushConf.Huawei.AppSecret, PushConf.Huawei.AppID)
if err != nil {
// HMS server error
LogError.Error("HMS server error: " + err.Error())
return false
}
res, err := client.SendMessage(context.Background(), notification)
if err != nil {
// Send Message error
LogError.Error("HMS server send message error: " + err.Error())
return false
}
// Huawei Push Send API does not support exact results for each token
if res.Code == "80000000" {
StatStorage.AddHuaweiSuccess(int64(1))
LogAccess.Debug("Huwaei Send Notification is completed successfully!")
} else {
isError = true
StatStorage.AddHuaweiError(int64(1))
LogAccess.Debug("Huawei Send Notification is failed! Code: " + res.Code)
}
if isError && retryCount < maxRetry {
retryCount++
// resend all tokens
goto Retry
}
return isError
}