-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmz_push.go
167 lines (149 loc) · 5 KB
/
mz_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
161
162
163
164
165
166
167
package PushSDK
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
type MZ struct {
AppSecret string `json:"app_secret"`
AppId string `json:"app_id"`
}
type MessageJson struct {
NoticeBarInfo NoticeBarInfo `json:"noticeBarInfo"`
NoticeExpandInfo NoticeExpandInfo `json:"noticeExpandInfo"`
ClickTypeInfo ClickTypeInfo `json:"clickTypeInfo"`
PushTimeInfo PushTimeInfo `json:"pushTimeInfo"`
}
type NoticeBarInfo struct {
NoticeBarType int `json:"noticeBarType"` // 通知栏样式 0-标准 2-安卓原生 【int 非必填,值为0】
Title string `json:"title"` // 推送标题 【string 必填,字数限制1-32字符】
Content string `json:"content"` // 推送内容 【string 必填,字数限制1-100字符】
}
type NoticeExpandInfo struct {
NoticeExpandType int `json:"noticeExpandType"` // 0-标准、1-文本 int 非必填
NoticeExpandContent string `json:"noticeExpandContent"` // 展示内容、为文本时必填
}
var clickTypeMZ = map[string]int{
"app": 0,
"url": 2,
"customize": 1,
}
type ClickTypeInfo struct {
ClickType int `json:"clickType"` // 点击动作 0-打开应用(默认)、1-打开应用页面、2-打开URI页面、3-应用客户端自定义
Url string `json:"url"` // clickType=2时必填
Parameters map[string]string `json:"parameters"` // json 格式 非必填
Activity string `json:"activity"` // clickType=1时必填。格式:pkg.activity
CustomAttribute string `json:"customAttribute"` // clickType=3时必填
}
type PushTimeInfo struct {
OffLine int `json:"offLine"` // 是否是离线消息 0-否、1-是(默认)
ValidTime int `json:"validTime"` // 有效时长(1-72小时内的正整数,默认24
}
//
//type AdvanceInfo struct {
// Suspend int `json:"suspend"`
// ClearNoticeBar int `json:"clearNoticeBar"`
// FixDisplay int `json:"fixDisplay"`
// FixStartDisplayTime string `json:"fixStartDisplayTime"`
// FixEndDisplayTime string `json:"fixEndDisplayTime"`
// NotificationType NotificationType `json:"notificationType"`
// NotifyKey string `json:"notifyKey"`
//}
//
//type NotificationType struct {
// Vibrate int `json:"vibrate"`
// Lights int `json:"lights"`
// Sound int `json:"sound"`
//}
func (mz *MZ) initMessage(m *Message) map[string]string {
var messageJson MessageJson
messageJson = MessageJson{
NoticeBarInfo: NoticeBarInfo{
NoticeBarType: 0,
Title: m.Title,
Content: m.Desc,
},
NoticeExpandInfo: NoticeExpandInfo{
NoticeExpandType: 0,
NoticeExpandContent: "",
},
ClickTypeInfo: ClickTypeInfo{
ClickType: clickTypeMZ[m.ClickType],
Url: m.ClickContent,
Parameters: map[string]string{},
Activity: m.ClickContent,
},
PushTimeInfo: PushTimeInfo{
OffLine: 0, // 是否进离线消息 否 是 【 非必填,默认值为 】
ValidTime: 0, // 有效时长(1-72小时内的正整数)【 int offline 值为1时,必填,默认24
},
}
messageJsonStr, _ := json.Marshal(messageJson)
var fields map[string]string
fields = map[string]string{
"messageJson": string(messageJsonStr),
}
return fields
}
const (
MZProductionHost = "http://server-api-push.meizu.com"
MZMessageURL = "/garcia/api/server/push/varnished/pushByPushId" // pushId推送
MZToAppURL = "/garcia/api/server/push/pushTask/pushToApp"
)
type MZResult struct {
Code string `json:"code"`
Message string `json:"message"`
Value string `json:"value"`
Redirect string `json:"redirect"`
MsgId string `json:"msgId"`
}
func (mz *MZ) SendMessage(m *Message, pushIds []string) (*Response, error) {
response := &Response{}
forms := mz.initMessage(m)
forms["appId"] = mz.AppId
forms["pushIds"] = strings.Join(pushIds, ",")
forms["sign"] = mz.generateSign(forms)
requestUrl := MZProductionHost + MZMessageURL
header := make(map[string]string)
body, err := postReqUrlencoded(requestUrl, forms, header)
if err != nil {
response.Code = HTTPERROR
return response, err
}
fmt.Println("result-mz", string(body))
var result = &MZResult{}
err = json.Unmarshal(body, result)
if err != nil {
}
if result.Code != MZSuccess && result.Value != "" {
response.Code = SendError
response.Reason = result.Message
response.ApnsId = result.MsgId
}
return response, nil
}
const (
MZSuccess = "200"
MZOtherError = "500"
MZSystemError = "1001"
MZServerBusy = "1003"
MZParamError = "1005"
MZAuthTokenError = "1006"
MZAppIdIllegal = "110000"
MZAppKeyIllegal = "110001"
MZParamEmpty = "110004"
MZAppInBlackList = "110009"
)
func (mz *MZ) generateSign(params map[string]string) string {
keys := make([]string, 0, len(params))
for k, _ := range params {
keys = append(keys, k)
}
str := ""
sort.Strings(keys)
for _, v := range keys {
str += fmt.Sprintf("%v=%v", v, params[v])
}
return md5Str(str + mz.AppSecret)
}