umeng.go
4.8 KB
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
package umeng
import (
"crypto/md5"
"encoding/json"
"fmt"
"github.com/astaxie/beego/httplib"
"io/ioutil"
"net/http"
"openapi/pkg/infrastructure/push"
"strings"
"time"
)
var (
Host = "http://msg.umeng.com"
ApiSend = "/api/send"
UploadPath = "/upload"
)
var FiledErrorFormat = "缺少字段:%v"
var FiledErrorFormatFunc = func(filed string) error {
return fmt.Errorf(FiledErrorFormat, filed)
}
const (
AppKey = "appkey"
Timestamp = "timestamp"
Type = "type"
ProductionMode = "production_mode"
DeviceTokens = "device_tokens"
Payload = "payload"
Policy = "policy"
Description = "description"
DisplayType = "displayType"
Body = "body"
Ticker = "ticker" // 必填,通知栏提示文字
Title = "title" // 必填,通知标题
Text = "text" //必填,通知文字描述
/*
// 可选,默认为"go_app",值可以为:
// "go_app": 打开应用
// "go_url": 跳转到URL
// "go_activity": 打开特定的activity
// "go_custom": 用户自定义内容。
*/
AfterOpen = "after_open"
Extra = "extra"
)
const (
Message = "message"
Notification = "notification"
)
const (
ExpireSpan = time.Second * 60 * 60 * 24
)
type SetPayload func(msg *UnicastMsg, option map[string]interface{}) error
type UmengNotification struct {
Auth *Auth
Request *httplib.BeegoHTTPRequest
}
func (notify *UmengNotification) Init(options ...push.Option) error {
return nil
}
func (notify *UmengNotification) Send(option map[string]interface{}) error {
var (
setPayload SetPayload = SetAndroidPayload
body []byte
err error
httpRsp *http.Response
ret = make(map[string]interface{})
data []byte
)
if option["DeviceType"].(int) == 1 {
setPayload = SetAndroidPayload
}
if body, err = notify.PostBody(option, setPayload); err != nil {
return err
}
notify.Request = httplib.Post(notify.Url(ApiSend, body))
notify.Request.Body(body)
if httpRsp, err = notify.Request.DoRequest(); err != nil {
return err
}
data, err = ioutil.ReadAll(httpRsp.Body)
defer httpRsp.Body.Close()
if err != nil {
return err
}
if err = json.Unmarshal(data, &ret); err != nil {
return err
}
if status, ok := ret["ret"]; ok {
if strings.EqualFold(fmt.Sprintf("%v", status), "SUCCESS") {
return nil
}
return fmt.Errorf("response:%v fail", ret)
}
return fmt.Errorf("response:%v invaild", ret)
}
func (notify *UmengNotification) PostBody(option map[string]interface{}, setPayload SetPayload) (body []byte, err error) {
var postData = new(UnicastMsg)
postData.AppKey = notify.Auth.AppKey
postData.Timestamp = fmt.Sprintf("%v", time.Now().Unix())
postData.ProductionMode = fmt.Sprintf("%v", true)
postData.Type = fmt.Sprintf("%v", option[Type])
postData.DeviceTokens = fmt.Sprintf("%v", option[DeviceTokens])
postData.Policy = MsgPolicy{ExpireTime: time.Now().Add(ExpireSpan).Format("2006-01-02 15:04:05")}
if err = setPayload(postData, option); err != nil {
return
}
postData.Description = fmt.Sprintf("%v", option[Title])
if body, err = json.Marshal(postData); err != nil {
return
}
return
}
func (notify *UmengNotification) Url(method string, body []byte) string {
var url string = fmt.Sprintf("%v%v", Host, method)
sign := fmt.Sprintf("%x", md5.Sum([]byte("POST"+url+string(body)+notify.Auth.AppMasterSecret)))
url = fmt.Sprintf("%v?sign=%v", url, sign)
return url
}
type UnicastMsg struct {
AppKey string `json:"appkey"`
Timestamp string `json:"timestamp"`
Type string `json:"type"` //发送类型 单播 列播...
ProductionMode string `json:"production_mode"`
DeviceTokens string `json:"device_tokens"`
Payload MsgPayload `json:"payload"`
Policy MsgPolicy `json:"policy"`
Description string `json:"description"`
}
type MsgPayload map[string]interface{}
type MsgPolicy struct {
ExpireTime string `json:"expire_time"`
}
//设置payload
func SetAndroidPayload(msg *UnicastMsg, option map[string]interface{}) error {
msg.Payload = make(map[string]interface{})
var (
displayType string
ok bool
)
if _, ok = option[DisplayType]; !ok {
return FiledErrorFormatFunc(DisplayType)
}
displayType = option[DisplayType].(string)
switch displayType {
case Message:
break
case Notification:
var payloadBody = make(map[string]interface{})
//if _,ok =option[Title];!ok{
// return FiledErrorFormatFunc(Title)
//}
//if _,ok =option[Text];!ok{
// return FiledErrorFormatFunc(Text)
//}
//if _,ok =option[Extra];ok{
// payloadBody[Extra]=option[Extra]
//}
payloadBody[Ticker] = option[Title]
payloadBody[Title] = option[Title]
payloadBody[Text] = option[Text]
payloadBody[AfterOpen] = "go_app"
msg.Payload[Body] = payloadBody
break
default:
return FiledErrorFormatFunc(DisplayType)
break
}
return nil
}