-
Notifications
You must be signed in to change notification settings - Fork 4
/
Mipush.php
342 lines (324 loc) · 9.98 KB
/
Mipush.php
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
namespace Mipush;
/**
* easy to use mipush parallelly
* version 0.1.0
* author: TIGERB <https://github.com/TIGERB>
* description: 目前只实现了按regid,alias(别名),user_account(用户账号),topic(标签), multi_topic(多标签),all(全体)推送
* date: 2016-09-28
*/
// -----------------------------------------------------------------------------------------------------------------------------
// 使用示例:
// try {
// Push::init(
// ['secret' => 'string,必传,ios密钥'],
// ['secret' => 'string,必传,android密钥', 'package_name' => 'string,必传,android包名'],
// [
// 'title' => 'string,非必传,消息通知自定义title',
// 'pass_through' => 'int,非必传,0通知栏消息,1透传,默认0',
// 'notify_type' => 'int,非必传,-1:默认所有,1:使用默认提示音提示,2:使用默认震动提示,4:使用默认led灯光提示',
// 'time_to_send' => 'int,非必传,定时推送,单位毫秒,默认0',
// ],
// 'string,develop开发环境,product生产环境, 默认develop'
// );
// $res = Push::toUse('string,小米push方法名', 'array, 该方法对应的参数');
// echo json_encode($res, JSON_UNESCAPED_UNICODE);
// } catch (Exception $e) {
// echo $e->getMessage();
// }
//
// this is a example:
// try {
// Push::init(
// ['secret' => ''],
// ['secret' => '', 'package_name' => 'com.test'],
// [
// 'title' => 'test',
// 'pass_through' => 0,
// 'notify_type' => -1,
// 'time_to_send' => 0,
// ],
// 'develop'
// );
// $res = Push::toUse('userAccount', [
// 'user_account' => [1],
// 'description' => 'test'
// ]);
// echo json_encode($res, JSON_UNESCAPED_UNICODE);
// } catch (Exception $e) {
// echo $e->getMessage();
// }
// -----------------------------------------------------------------------------------------------------------------------------
/**
* easy mipush sdk
**/
class Mipush
{
private $_environment = 'develop';
private $_osType = '';
private $_host = '';
private $_headers = '';
private $_url = '';
private $_function = [];
private $_data = [];
private $_options = [
'title' => '消息通知自定义title',
'restricted_package_name' => '',
'pass_through' => 0, // 0 通知栏消息 1 透传
'notify_type' => -1, // -1:默认所有,1:使用默认提示音提示,2:使用默认震动提示,4:使用默认led灯光提示
'time_to_send' => 0, // 定时推送 单位毫秒 默认0
];
private static $_environmentConfig = [
'domain' => [
'product' => 'https://api.xmpush.xiaomi.com/',
'develop' => 'https://sandbox.xmpush.xiaomi.com/'
],
];
/* mipush uri 参考*/
// const reg_url = '/v3/message/regid';
// const alias_url = '/v3/message/alias';
// const user_account_url = '/v2/message/user_account';
// const topic_url = '/v3/message/topic';
// const multi_topic_url = '/v3/message/multi_topic';
// const all_url = '/v3/message/all';
// const multi_messages_regids_url = '/v2/multi_messages/regids';
// const multi_messages_aliases_url = '/v2/multi_messages/aliases';
// const multi_messages_user_accounts_url = '/v2/multi_messages/user_accounts';
// const stats_url = '/v1/stats/message/counters';
// const message_trace_url = '/v1/trace/message/status';
// const messages_trace_url = '/v1/trace/messages/status';
// const validation_regids_url = '/v1/validation/regids';
// const subscribe_url = '/v2/topic/subscribe';
// const unsubscribe_url = '/v2/topic/unsubscribe';
// const subscribe_alias_url = '/v2/topic/subscribe/alias';
// const unsubscribe_alias_url = '/v2/topic/unsubscribe/alias';
// const fetch_invalid_regids_url = 'https://feedback.xmpush.xiaomi.com/v1/feedback/fetch_invalid_regids';
// const delete_schedule_job = '/v2/schedule_job/delete';
// const check_schedule_job_exist = '/v2/schedule_job/exist';
// const get_all_aliases = '/v1/alias/all';
// const get_all_topics = '/v1/topic/all';
/**
* 小米推送接口信息定义
*
* url/请求参数
* @var array
*/
private $_functionDefine = [
'regid' => [
'uri' => 'v3/message/regid',
'arguments' => [
'registration_id' => [
'type' => 'array',
'must' => 'y'
],
'description' => [
'type' => 'string',
'must' => 'y'
],
'params' => [//自定义参数
'type' => 'array',
'must' => 'n'
],
]
],
'userAccount' => [
'uri' => 'v2/message/user_account',
'arguments' => [
'user_account' => [
'type' => 'array',
'must' => 'y'
],
'description' => [
'type' => 'string',
'must' => 'y'
],
'params' => [//自定义参数
'type' => 'array',
'must' => 'n'
],
]
],
'alias' => [
'uri' => 'v3/message/alias',
'arguments' => [
'alias' => [
'type' => 'array',
'must' => 'y'
],
'description' => [
'type' => 'string',
'must' => 'y'
],
'params' => [//自定义参数
'type' => 'array',
'must' => 'n'
],
]
],
'topic' => [
'uri' => 'v3/message/topic',
'arguments' => [
'topics' => [
'type' => 'array',
'must' => 'y'
],
'description' => [
'type' => 'string',
'must' => 'y'
],
'params' => [//自定义参数
'type' => 'array',
'must' => 'n'
],
]
],
'multiTopic' => [
'uri' => 'v3/message/multi_topic',
'arguments' => [
'topics' => [
'type' => 'array',
'must' => 'y'
],
'topics_op' => [// UNION并集,INTERSECTION交集,EXCEPT差集
'type' => 'string',
'must' => 'y'
],
'description' => [
'type' => 'string',
'must' => 'y'
],
'params' => [//自定义参数
'type' => 'array',
'must' => 'n'
],
]
],
'all' => [
'uri' => 'v3/message/all',
'arguments' => [
'description' => [
'type' => 'string',
'must' => 'y'
],
'params' => [//自定义参数
'type' => 'array',
'must' => 'n'
],
]
],
];
/**
* 初始化配置
*
* @param $string $os_type 系统类型
* @param $string $config 配置
* @param array $options 设置 [
* 'title' => 'string,标题',
* 'pass_through' => 'tinyint,0通知栏消息,1透传,默认0'
* 'notify_type' => 'tinyint,-1,1,2,3,4',
* 'time_to_send' => 'int, 定时推送, 毫秒'
* ]
* @param string $environment 环境
*/
public function __construct($os_type='', $config=array(), $options=array(), $environment='')
{
/* init environment */
if ($environment) {
$this->_environment = $environment;
}
if ($os_type === 'ios') {
$this->_host = self::$_environmentConfig['domain'][$this->_environment];// ios
}else{
$this->_host = self::$_environmentConfig['domain']['product'];// android
}
/* init option */
$this->_headers = [];
$this->_headers[] = 'Authorization: key=' . $config['secret'];
if ($os_type === 'android') {
$this->_options['restricted_package_name'] = $config['package_name'];
}
foreach ($this->_options as $k => &$v) {
if (in_array($k, $options)) {
$v = $options[$k];
}
}
}
/**
* 请求参数校验
*
* @param array $arguments 请求参数
* @return mixed void||object
*/
private function dataCheck($arguments=array())
{
foreach ($this->_function['arguments'] as $k => $v) {
if ($v['must'] === 'y' && !$arguments[$k]) {
throw new \Exception("$k is must argument", 400);
}
if ($arguments[$k] && gettype($arguments[$k]) !== $v['type']) {
throw new \Exception("$k type is $v", 400);
}
}
}
/**
* 魔法方法
* 获取类属性
*
* @param string $name 类属型名称
* @return mixed
*/
public function __get($name='')
{
return $this->$name;
}
/**
* 魔术方法
*
* 重载对象方法
* @param string $name 小米推送方法名称
* @param array $arguments 请求参数
* @return mixed void||object
*/
public function __call($name,$arguments)
{
$arguments = $arguments[0];
$this->_function = $this->_functionDefine[$name];
$this->_url = $this->_host . $this->_function['uri'];
$this->dataCheck($arguments);
switch ($name) {
case 'regid':
$this->_data['registration_id'] = $arguments['registration_id'];
break;
case 'userAccount':
$this->_data['user_account'] = implode(',', $arguments['user_account']);
break;
case 'alias':
$this->_data['alias'] = implode(',', $arguments['alias']);
break;
case 'topic':
$this->_data['topic'] = $arguments['topic'];
break;
case 'multiTopic':
$this->_data['topics'] = implode(";$;", $arguments['topics']);
$this->_data['topic_op'] = $arguments['topic_op'];
break;
case 'all':
$this->_data['topics'] = implode(";$;", $arguments['topics']);
$this->_data['topic_op'] = 'UNION';
break;
default:
throw new \Exception('Sorry, This function is useless in this version', 404);
break;
}
$this->_data['title'] = $this->_options['title'];
$this->_data['description'] = $arguments['description'];
if($arguments['params']) {
foreach ($arguments['params'] as $k => $v) {
$this->_data['extra.'.$k] = $v;// 自定义参数
}
}
if ($this->_osType === 'android') {
$this->_data = array_merge($this->_data, $this->_options);
}
}
}