-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxmpush_test.go
207 lines (168 loc) · 4.01 KB
/
xmpush_test.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
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
package xmpush
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"testing"
)
var Cfg = &XmpushConfig{
AppSecret: "",
Package: "",
}
type XmpushConfig struct {
AppSecret string `toml:"app_secret"`
Package string `toml:"package"`
}
//消息payload,根据业务自定义
type Payload struct {
PushTitle string `json:"push_title"`
PushBody string `json:"push_body"`
IsShowNotify string `json:"is_show_notify"`
Ext string `json:"ext"`
}
//测试单推
func TestXmPush_SendByCid(t *testing.T) {
iXmPush, err := NewXmPush(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
cid := "xxx"
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = iXmPush.SendByCid(cid, &payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
//测试群推
func TestXmPush_SendByCids(t *testing.T) {
iXmPush, err := NewXmPush(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
cids := []string{"xxx"}
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = iXmPush.SendByCids(cids, &payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
//测试全推
func TestXmPush_SendAll(t *testing.T) {
iXmPush, err := NewXmPush(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = iXmPush.SendAll(&payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
type XMPush struct {
Config *XmpushConfig
}
//获取实例
func NewXmPush(config *XmpushConfig) (*XMPush, error) {
if config.Package == "" || config.AppSecret == "" {
return nil, errors.New("请检查配置")
}
xm := &XMPush{
Config: config,
}
return xm, nil
}
//根据用户cid推送
func (m *XMPush) SendByCid(cid string, payload *Payload) error {
return m.SendByCids([]string{cid}, payload)
}
//根据用户cids批量推送
func (m *XMPush) SendByCids(cids []string, payload *Payload) error {
payload_str, _ := json.Marshal(payload)
//是否透传
passThrough := 1
if payload.IsShowNotify == "1" {
passThrough = 0 //通知栏消息
}
message := &Message{
Payload: string(payload_str),
Title: payload.PushTitle,
Description: payload.PushBody,
PassThrough: int32(passThrough),
NotifyType: 1,
RestrictedPackageName: m.Config.Package,
Extra: map[string]string{
"notify_foreground": "1",
},
}
message.RegistrationId = strings.Join(cids, ",")
result, err := SendMessageByRegIds(m.Config.AppSecret, message)
if err != nil {
return err
}
fmt.Println(result)
return nil
}
//根据别名推送批量推送
func (m *XMPush) SendByAliases(aliases []string, payload *Payload) error {
payload_str, _ := json.Marshal(payload)
//是否透传
passThrough := 1
if payload.IsShowNotify == "1" {
passThrough = 0 //通知栏消息
}
message := &Message{
Payload: string(payload_str),
Title: payload.PushTitle,
Description: payload.PushBody,
PassThrough: int32(passThrough),
NotifyType: 1,
RestrictedPackageName: m.Config.Package,
Extra: map[string]string{
"notify_foreground": "1",
},
}
message.Alias = strings.Join(aliases, ",")
result, err := SendMessageByRegAliasIds(m.Config.AppSecret, message)
if err != nil {
return err
}
fmt.Println(result)
return nil
}
//推送给所有人
func (m *XMPush) SendAll(payload *Payload) error {
payload_str, _ := json.Marshal(payload)
//是否透传
passThrough := 1
if payload.IsShowNotify == "1" {
passThrough = 0 //通知栏消息
}
message := &Message{
Payload: string(payload_str),
Title: payload.PushTitle,
Description: payload.PushBody,
PassThrough: int32(passThrough),
NotifyType: 1,
RestrictedPackageName: m.Config.Package,
Extra: map[string]string{
"notify_foreground": "1",
},
}
result, err := SendMessageAll(m.Config.AppSecret, message)
if err != nil {
return err
}
fmt.Println(result)
return nil
}