-
Notifications
You must be signed in to change notification settings - Fork 11
/
hw_push.go
160 lines (137 loc) · 4.39 KB
/
hw_push.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
package PushSDK
import (
"encoding/json"
"fmt"
"sync"
)
type HW struct {
sync.Mutex
AppId string `json:"app_id"`
ClientSecret string `json:"client_secret"`
}
type HWFields struct {
Message MessageNotification `json:"message"`
}
type MessageNotification struct {
Notification Notification `form:"notification" json:"notification"`
Android Android `form:"android" json:"android"`
Token []string `json:"token"`
}
type Notification struct {
Title string `form:"title" json:"title"`
Body string `form:"body" json:"body"`
}
type Android struct {
CollapseKey int `json:"collapse_key"` // 缓存。0-只缓存新的一条,-1对所有离线消息都缓存、1-100分组缓存
Notification AndroidNotification `json:"notification"`
}
type AndroidNotification struct {
Title string `form:"title" json:"title"`
Body string `form:"body" json:"body"`
Icon string `json:"icon"` // 小图标
Tag string `json:"tag"` // 消息标签
ClickAction ClickAction `json:"click_action"`
//Badge BadgeNotification `json:"badge"` // 角标
}
var clickTypeHW = map[string]int{
"app": 3,
"url": 2,
"customize": 1,
}
type ClickAction struct {
Type int `json:"type"` // 1-打开应自定义页面、2-打开URL、3-打开应用APP
Intent string `json:"intent"` // 自定义页面中intent的实现
Url string `json:"url"`
Action string `json:"action"` // 设置通过action打开应用自定义页面时,本字段填写要打开的页面activity对应的action。
}
type BadgeNotification struct {
AddNum int `json:"add_num"`
Class string `json:"class"` // 应用入口Activity类全路径。 样例:com.example.hmstest.MainActivity
SetNum int `json:"set_num"` // 角标设置数字,大于等于0小于100的整数。如果set_num与add_num同时存在时,以set_num为准
}
func (h *HW) initMessage(m *Message, token []string) string {
fields := HWFields{
Message: MessageNotification{
Notification: Notification{
Title: m.Title,
Body: m.Desc,
},
Android: Android{
Notification: AndroidNotification{
Title: m.Title,
Body: m.Desc,
ClickAction: ClickAction{
Type: clickTypeHW[m.ClickType],
Url: m.ClickContent,
Intent: "#Intent;compo=com.rvr/.Activity;S.W=U;end",
},
Tag: m.ApnsId,
},
},
Token: token,
},
}
fieldsStr, _ := json.Marshal(fields)
return string(fieldsStr)
}
const (
HWProductionHost = "https://push-api.cloud.huawei.com/v1/"
HWMessageURL = "/messages:send"
HWTokenURL = "https://oauth-login.cloud.huawei.com/oauth2/v3/token"
HWGrantType = "client_credentials"
)
const (
HWSuccess = "80000000"
HWPartTokenSendSuccess = "80100000"
)
type HWResult struct {
Code string `json:"code"` //80000000表示成功,非80000000表示失败
Msg string `json:"msg"` //错误码描述
RequestId string `json:"requestId,omitempty"` //请求标识。
}
func (h *HW) SendMessage(m *Message, token []string) (*Response, error) {
response := &Response{}
forms := h.initMessage(m, token)
requestUrl := HWProductionHost + h.AppId + HWMessageURL
header := make(map[string]string)
accessToken := getAccessToken(h.AppId, h.ClientSecret)
header["Authorization"] = fmt.Sprintf("Bearer %s", accessToken)
body, err := postReqJson(requestUrl, forms, header)
fmt.Println("result-hw", string(body))
if err != nil {
response.Code = HTTPERROR
return response, err
}
var result = &HWResult{}
err = json.Unmarshal(body, result)
if err != nil {
}
if result.Code != HWSuccess {
response.Code = SendError
response.Reason = result.Msg
response.ApnsId = result.RequestId
}
return response, err
}
type TokenResult struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
}
func getAccessToken(clientId, clientSecret string) string {
var accessToken string
requestPath := HWTokenURL
forms := make(map[string]string)
forms["grant_type"] = HWGrantType
forms["client_id"] = clientId
forms["client_secret"] = clientSecret
header := make(map[string]string)
body, err := postReqUrlencoded(requestPath, forms, header)
var result = &TokenResult{}
err = json.Unmarshal(body, result)
if err != nil {
fmt.Println("getAccessToken Unmarshal", err)
}
accessToken = result.AccessToken
return accessToken
}