forked from xxgail/PushSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oppo_push.go
159 lines (138 loc) · 4.6 KB
/
oppo_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
package PushSDK
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type OPPO struct {
AppKey string `json:"app_key"`
MasterSecret string `json:"master_secret"`
}
type MessageFields struct {
TargetType int `json:"target_type"`
TargetValue string `json:"target_value"`
Notification OPPONotification `json:"notification"`
}
var clickTypeOPPO = map[string]int{
"app": 0,
"url": 2,
"customize": 1,
}
type OPPONotification struct {
AppMessageId string `json:"app_message_id"` // 消息tag
Style int `json:"style"` // 1-标准样式(默认1) 2-长文本 3-大图
BigPictureId string `json:"big_picture_id"` // 大图id
SmallPictureId string `json:"small_picture_id"` // 图标
Title string `json:"title"`
SubTitle string `json:"sub_title"`
Content string `json:"content"`
ClickActionType int `json:"click_action_type"` //点击动作类型0,启动应用;1,打开应用内页(activity的intent action);2,打开网页;4,打开应用内页(activity);【非必填,默认值为0】;5,Intent scheme URL
ClickActionActivity string `json:"click_action_activity"` // 应用内页地址【click_action_type为1/4/ 时必填,长度500】
ClickActionUrl string `json:"click_action_url"` // 网页地址或【click_action_type为2与5时必填
ActionParameters string `json:"action_parameters"` // 传递给网页或应用的参数 json 格式
}
func (o *OPPO) initMessage(m *Message, registrationIds []string) map[string]string {
var messages []MessageFields
for _, v := range registrationIds {
message := MessageFields{
TargetType: 2,
TargetValue: v,
Notification: OPPONotification{
AppMessageId: m.ApnsId,
Style: 1,
Title: m.Title,
SubTitle: "",
Content: m.Desc,
ClickActionType: clickTypeOPPO[m.ClickType],
ClickActionActivity: m.ClickContent,
ClickActionUrl: m.ClickContent,
},
}
messages = append(messages, message)
}
messagesStr, _ := json.Marshal(messages)
fields := map[string]string{
"messages": string(messagesStr),
}
return fields
}
const (
OPPOProductionHost = "https://api.push.oppomobile.com"
OPPOMessageURL = "/server/v1/message/notification/unicast_batch" // pushId推送
OPPOTokenURL = "/server/v1/auth"
)
const (
OPPOSuccess = 0
OPPOServiceInFlowControl = -2
OPPOServiceCurrentlyUnavailable = -1
OPPOInvalidAuthToken = 11
OPPOHttpActionNotAllowed = 12
OPPOAppCallLimited = 13
)
type OPPOResult struct {
Code int `json:"code"`
Message string `json:"message"`
Data []Data `json:"data"`
}
type Data struct {
MessageId string `json:"messageId"`
RegistrationId string `json:"registrationId"`
ErrorCode int `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
}
func (o *OPPO) SendMessage(m *Message, pushIds []string) (*Response, error) {
response := &Response{}
forms := o.initMessage(m, pushIds)
requestUrl := OPPOProductionHost + OPPOMessageURL
header := make(map[string]string)
header["auth_token"], _ = o.getAuthTokenOPPO()
body, err := postReqUrlencoded(requestUrl, forms, header)
if err != nil {
response.Code = HTTPERROR
return response, err
}
fmt.Println("result-oppo", string(body))
var result = &OPPOResult{}
err = json.Unmarshal(body, result)
if err != nil {
}
if result.Code != OPPOSuccess {
response.Code = SendError
response.Reason = result.Message
}
return response, nil
}
type OPPOTokenResult struct {
Code int `json:"code"`
Message string `json:"message"`
Data OPPOTokenData `json:"data"`
}
type OPPOTokenData struct {
AuthToken string `json:"auth_token"`
CreateTime int `json:"create_time"`
}
func (o *OPPO) getAuthTokenOPPO() (string, error) {
var authToken string
// 毫秒级
currentTimeStr := strconv.FormatInt(time.Now().UnixNano()/(1e6), 10)
requestUrl := OPPOProductionHost + OPPOTokenURL
forms := make(map[string]string)
forms["app_key"] = o.AppKey
forms["sign"] = sha256Encode(o.AppKey + currentTimeStr + o.MasterSecret)
forms["timestamp"] = currentTimeStr
header := make(map[string]string)
body, err := postReqUrlencoded(requestUrl, forms, header)
if err != nil {
}
var result = &OPPOTokenResult{}
err = json.Unmarshal(body, result)
if err != nil {
fmt.Println("getAuthToken Unmarshal", err)
}
if result.Code != 0 {
fmt.Println("请求AuthToken结果错误:", result)
}
authToken = result.Data.AuthToken
return authToken, err
}