model.go
6.6 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
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 getui
import (
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
"openapi/pkg/infrastructure/push"
)
//1.消息模板
type NotificationTemplate struct {
*Template
Notification *Notification `json:"notification"`
}
//1.新建通知模板
func NewNotificationTemplate(options *push.Options) *NotificationTemplate {
return &NotificationTemplate{
Template: NewTemplate(options),
Notification: &Notification{
Style: (&Style{}).SetStyle0(options),
Transmission: NewTransmission(options),
},
}
}
//2.透传模板
type TransmissionTemplate struct {
*Template
Transmission *Transmission `json:"transmission"`
PushInfo *PushInfo `json:"push_info"`
}
//2.新建透传模板
func NewTransmissionTemplate(options *push.Options) *TransmissionTemplate {
return &TransmissionTemplate{
Template: NewTemplate(options),
Transmission: NewTransmission(options),
PushInfo: NewPushInfo(options),
}
}
type Template struct {
ClientId string `json:"cid,omitempty"`
RequestId string `json:"requestid,omitempty"`
Message *Message `json:"message"`
}
type Notification struct {
Style *Style `json:"style"`
*Transmission
}
type Message struct {
AppKey string `json:"appkey"`
IsOffline bool `json:"is_offline"`
MsgType string `json:"msgtype"`
OfflineExpireTime int `json:"offline_expire_time"` //offline_expire_time
}
//透传
type Transmission struct {
TransmissionType bool `json:"transmission_type"` //收到消息是否立即启动应用,true为立即启动,false则广播等待启动,默认是否
TransmissionContent string `json:"transmission_content,omitempty"` //透传内容
DurationBegin string `json:"duration_begin,omitempty"`
DurationEnd string `json:"duration_end,omitempty"`
Notify interface{} `json:"notify,omitempty"`
}
func (o *Transmission) SetTransmissionType(t bool) {
o.TransmissionType = t
}
func (o *Transmission) SetTransmissionContent(s string) {
o.TransmissionContent = s
}
func (o *Transmission) SetDuration(begin, end string) {
o.DurationBegin = begin
o.DurationEnd = end
}
func (o *Transmission) SetNotify(options *push.Options) {
mapNotify := make(map[string]interface{})
mapNotify["title"] = options.Title
mapNotify["content"] = options.Content
// 安卓设备生成
mapNotify["intent"] = options.Intent
mapNotify["type"] = 1
//mapNotify["extKVList"] = []ExtKVList{
// //{Constrains: "HW", Key: "/message/android/notification/badge/add_num", Value: 1},
// //{Constrains: "HW", Key: "/message/android/notification/badge/class", Value: `"com.getui.demo.GetuiSdkDemoActivity"`},
// // 小米厂家支持
// {Constrains: "XM", Key: "channel", Value: "\"2882303761518034255\""},//Default 2882303761518034255
//}
o.Notify = mapNotify
}
type ExtKVList struct {
Constrains string `json:"constrains"`
Key string `json:"key"`
Value interface{} `json:"value"`
}
func NewTemplate(options *push.Options) *Template {
return &Template{
Message: NewMessage(options),
ClientId: options.ClientId,
RequestId: genRequestId(),
}
}
func NewTransmission(options *push.Options) *Transmission {
t := &Transmission{}
if len(options.TransmissionContent) == 0 {
//t.SetTransmissionType(false)
return t
}
t.SetTransmissionType(false)
t.SetTransmissionContent(options.TransmissionContent)
t.SetNotify(options)
return t
}
func NewMessage(options *push.Options) *Message {
return &Message{
AppKey: options.AppKey,
IsOffline: true,
MsgType: resolveMsgType(options.MsgType),
OfflineExpireTime: 100000000,
}
}
func resolveMsgType(msgType int) string {
/*
消息应用类型,
可选项:notification、link、notypopload、startactivity, transmission
*/
switch msgType {
case push.SystemNotification:
return "notification"
case push.SystemTransmission:
return "transmission"
}
return "notification"
}
func genRequestId() string {
return uid.NewV1().StringNoDash()
}
//样式 0:系统样式 1:个推样式 4:纯图样式 6:展开通知样式
type Style struct {
Type int `json:"type"` //样式类型
Text string `json:"text"` //通知内容
Title string `json:"title"` //通知标题
Logo string `json:"logo,omitempty"` //通知的图标名称,包含后缀名(需要在客户端开发时嵌入),如“push.png”
//IsRing bool `json:"is_ring"` //收到通知是否响铃:true响铃,false不响铃。默认响铃
//IsVibrate bool `json:"is_vibrate"` //收到通知是否振动:true振动,false不振动。默认振动
NotifyId int `json:"notify_id"` //需要被覆盖的消息已经增加了notifyId字段,用于实现下发消息的覆盖。新的消息使用相同的notifyId下发。
}
//设置默认样式 0
func (s *Style) SetStyle0(options *push.Options) *Style {
s.Type = 0
s.Title = options.Title
s.Text = options.Content
s.Logo = "push.png" //TODO:设置Logo地址
s.NotifyId = 1
return s
}
//认证请求/应答
type AuthSignRequest struct {
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
AppKey string `json:"appkey"`
}
type AuthSignResponse struct {
*Result
AuthToken string `json:"auth_token"`
}
//应答结果
type Result struct {
Result string `json:"result"`
TaskId string `json:"taskid"`
Status string `json:"status"`
Desc string `json:"desc"`
}
//透传附加的推送信息
func NewPushInfo(options *push.Options) (v *PushInfo) {
v = &PushInfo{
Aps: NewAps(options),
Payload: options.TransmissionContent,
}
return
}
func NewAps(options *push.Options) (v *Aps) {
v = &Aps{
Alert: NewAlert(options),
AutoBadge: "+1",
ContentAvailable: 0,
Sound: "default",
}
// 声音
if value, ok := options.GetExt("sound"); ok {
v.Sound = value.(string)
}
return
}
func NewAlert(options *push.Options) (v *Alert) {
v = &Alert{
//Title: options.Title, //TODO:去掉这个ios通知栏只有内容行,没有标题行,如果后期需要显示这个 需要ext里面扩展字段用来控制是否显示标题
Body: options.Content,
}
return
}
type PushInfo struct {
Aps *Aps `json:"aps"`
Payload string `json:"payload,omitempty"`
}
type Aps struct {
Alert *Alert `json:"alert"`
AutoBadge string `json:"autoBadge"` //用于计算应用上面未读数字
ContentAvailable int `json:"content-available,omitempty"` //推送直接带有透传数据 0:有通知栏消息 1:无通知栏消息
Sound string `json:"sound"` // 推送声音 storein_voice.mp3
}
type Alert struct {
Title string `json:"title"`
Body string `json:"body"`
}