bhq@iemsoft.cn
2018-11-27 e2b48dac099e43f4b3243cdf19a7522e4b5eccbe
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
 
namespace JPush;
 
use Httpful\Request;
use Httpful\Exception\ConnectionErrorException;
use JPush\Exception\APIConnectionException;
use JPush\Model\PushPayload;
use JPush\Model\ReportResponse;
use JPush\Model\MessageResponse;
use JPush\Model\UserResponse;
use JPush\Model\DeviceResponse;
 
use InvalidArgumentException;
 
class JPushClient {
    const PUSH_URL = 'https://api.jpush.cn/v3/push';
    const REPORT_URL = 'https://report.jpush.cn/v2/received';
    const VALIDATE_URL = 'https://api.jpush.cn/v3/push/validate';
    const MESSAGES_URL = 'https://report.jpush.cn/v3/messages';
    const USERS_URL = 'https://report.jpush.cn/v3/users';
    const DEVICES_URL = 'https://device.jpush.cn/v3/devices/{registration_id}';
    const ALL_TAGS_URL = 'https://device.jpush.cn/v3/tags/';
    const IS_IN_TAG_URL = 'https://device.jpush.cn/v3/tags/{tag}/registration_ids/{registration_id}';
    const TAG_URL = 'https://device.jpush.cn/v3/tags/{tag}';
    const ALIAS_URL = 'https://device.jpush.cn/v3/aliases/{alias}';
 
    const USER_AGENT = 'JPush-API-PHP-Client';
    const CONNECT_TIMEOUT = 5;
    const READ_TIMEOUT = 30;
    const DEFAULT_MAX_RETRY_TIMES = 3;
 
    public $appKey;
    public $masterSecret;
    public $retryTimes;
 
    public function __construct($appKey, $masterSecret, $retryTimes=self::DEFAULT_MAX_RETRY_TIMES)
    {
        if (is_null($appKey) || is_null($masterSecret)) {
            throw new InvalidArgumentException("appKey and masterSecret must be set.");
        }
 
        if (!is_string($appKey) || !is_string($masterSecret)) {
            throw new InvalidArgumentException("Invalid appKey or masterSecret");
        }
        $this->appKey = $appKey;
        $this->masterSecret = $masterSecret;
        $this->retryTimes = $retryTimes;
    }
 
    public function push() {
        return new PushPayload($this);
    }
 
 
    /*----Report API start----*/
    public function report($msg_id) {
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = self::REPORT_URL . '?msg_ids=' . $msg_id;
        $response = $this->request($url, null, $header, 'GET');
        return new ReportResponse($response);
    }
 
    public function messages($msg_id) {
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = self::MESSAGES_URL . '?msg_ids=' . $msg_id;
        $response = $this->request($url, null, $header, 'GET');
        return new MessageResponse($response);
    }
 
    public function users($time_unit, $start, $duration) {
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = self::USERS_URL . '?time_unit=' . $time_unit . '&start=' . $start . '&duration=' . $duration;
        $response = $this->request($url, null, $header, 'GET');
        return new UserResponse($response);
    }
    /*----Report API end----*/
 
 
    /*----Device API start----*/
    /**
     * 获取指定RegistrationId的所有属性,包含tags, alias。
     * @param $registrationId
     * @return DeviceResponse
     */
    public function getDeviceTagAlias($registrationId) {
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
        $response = $this->request($url, null, $header, 'GET');
        return new DeviceResponse($response);
    }
 
    /**
     * 移除指定RegistrationId的所有tag
     * @param $registrationId
     * @return DeviceResponse
     * @throws \InvalidArgumentException
     */
    public function removeDeviceTag($registrationId) {
        if (is_null($registrationId) || !is_string($registrationId)) {
            throw new InvalidArgumentException("Invalid registrationId string");
        }
        $payload = array('tags'=>'');
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
        $response = $this->request($url, json_encode($payload), $header, 'POST');
        return new DeviceResponse($response);
    }
 
    /**
     * 移除指定RegistrationId的所有alias
     * @param $registrationId
     * @return DeviceResponse
     * @throws \InvalidArgumentException
     */
    public function removeDeviceAlias($registrationId) {
        if (is_null($registrationId) || !is_string($registrationId)) {
            throw new InvalidArgumentException("Invalid registrationId string");
        }
        $payload = array('alias'=>'');
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
        $response = $this->request($url, json_encode($payload), $header, 'POST');
        return new DeviceResponse($response);
    }
 
    /**
     * 更新指定RegistrationId的指定属性,当前支持tags, alias
     * @param $registrationId
     * @param null $alias
     * @param null $addTags
     * @param null $removeTags
     * @return DeviceResponse
     * @throws \InvalidArgumentException
     */
    public function updateDeviceTagAlias($registrationId, $alias = null, $addTags = null, $removeTags = null) {
        $payload = array();
 
        if (is_null($registrationId) || !is_string($registrationId)) {
            throw new InvalidArgumentException("Invalid registrationId string");
        }
 
        $aliasIsNull = is_null($alias);
        $addTagsIsNull = is_null($addTags);
        $removeTagsIsNull = is_null($removeTags);
 
        if ($aliasIsNull && $addTagsIsNull && $removeTagsIsNull) {
            throw new InvalidArgumentException("alias, addTags, removeTags not all null");
        }
 
        if (!$aliasIsNull) {
            if (is_string($alias)) {
                $payload['alias'] = $alias;
            } else {
                throw new InvalidArgumentException("Invalid alias string");
            }
        }
 
        $tags = array();
 
        if (!$addTagsIsNull) {
           if (is_array($addTags)) {
               $tags['add'] = $addTags;
           } else {
               throw new InvalidArgumentException("Invalid addTags array");
           }
        }
 
        if (!$removeTagsIsNull) {
            if (is_array($removeTags)) {
                $tags['remove'] = $removeTags;
            } else {
                throw new InvalidArgumentException("Invalid removeTags array");
            }
        }
 
        if (count($tags) > 0) {
            $payload['tags'] = $tags;
        }
 
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
        $response = $this->request($url, json_encode($payload), $header, 'POST');
        return new DeviceResponse($response);
    }
 
    /**
     * 获取当前应用的所有标签列表
     * @return DeviceResponse
     */
    public function getTags() {
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $response = $this->request(self::ALL_TAGS_URL, null, $header, 'GET');
        return new DeviceResponse($response);
    }
 
    /**
     * 查询某个用户是否在tag下
     * @param $registrationId
     * @param $tag
     * @return DeviceResponse
     * @throws \InvalidArgumentException
     */
    public function isDeviceInTag($registrationId, $tag) {
        if (is_null($registrationId) || !is_string($registrationId)) {
            throw new InvalidArgumentException("Invalid registrationId string");
        }
 
        if (is_null($tag) || !is_string($tag)) {
            throw new InvalidArgumentException("Invalid tag string");
        }
 
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
 
        $url = str_replace('{tag}', $tag, self::IS_IN_TAG_URL);
        $url = str_replace('{registration_id}', $registrationId, $url);
        $response = $this->request($url, null, $header, 'GET');
        return new DeviceResponse($response);
    }
 
    /**
     * 对指定tag添加或者删除registrationId
     * @param $tag
     * @param null $addDevices
     * @param null $removeDevices
     * @return \Httpful\associative|null|string
     * @throws \InvalidArgumentException
     */
    public function updateTagDevices($tag, $addDevices = null, $removeDevices = null) {
        if (is_null($tag) || !is_string($tag)) {
            throw new InvalidArgumentException("Invalid tag string");
        }
 
        $addDevicesIsNull = is_null($addDevices);
        $removeDevicesIsNull = is_null($removeDevices);
 
        if ($addDevicesIsNull && $removeDevicesIsNull) {
            throw new InvalidArgumentException("Either or both addDevices and removeDevices must be set.");
        }
 
        $registrationId = array();
 
        if (!$addDevicesIsNull) {
            if (is_array($addDevices)) {
                $registrationId['add'] = $addDevices;
            } else {
                throw new InvalidArgumentException("Invalid addDevices array");
            }
        }
 
        if (!$removeDevicesIsNull) {
            if (is_array($removeDevices)) {
                $registrationId['remove'] = $removeDevices;
            } else {
                throw new InvalidArgumentException("Invalid removeDevices array");
            }
        }
 
        $payload = array('registration_ids'=>$registrationId);
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = str_replace('{tag}', $tag, self::TAG_URL);
        $response = $this->request($url, json_encode($payload), $header, 'POST');
        return new DeviceResponse($response);
    }
 
    /**
     * 删除指定Tag,以及与其关联的用户之间的关联关系
     * @param $tag
     * @return DeviceResponse
     * @throws \InvalidArgumentException
     */
    public function deleteTag($tag) {
        if (is_null($tag) || !is_string($tag)) {
            throw new InvalidArgumentException("Invalid tag string");
        }
 
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = str_replace('{tag}', $tag, self::TAG_URL);
        $response = $this->request($url, null, $header, 'DELETE');
        return new DeviceResponse($response);
    }
 
    /**
     * 获取指定alias下的用户,最多输出10个
     * @param $alias
     * @param null $platform
     * @return DeviceResponse
     * @throws \InvalidArgumentException
     */
    public function getAliasDevices($alias, $platform = null) {
        if (is_null($alias) || !is_string($alias)) {
            throw new InvalidArgumentException("Invalid alias string");
        }
 
        $url = str_replace('{alias}', $alias, self::ALIAS_URL);
 
        if (!is_null($platform)) {
            if (is_array($platform)) {
                $isFirst = true;
                foreach($platform as $item) {
                    if ($isFirst) {
                        $url = $url . '?platform=' . $item;
                        $isFirst = false;
                    } else {
                        $url = $url . ',' . $item;
                    }
                }
            } else {
                throw new InvalidArgumentException("Invalid platform array");
            }
        }
 
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $response = $this->request($url, null, $header, 'GET');
        return new DeviceResponse($response);
    }
 
    /**
     * 删除指定alias,以及该alias与用户的绑定关系
     * @param $alias
     * @return DeviceResponse
     * @throws \InvalidArgumentException
     */
    public function deleteAlias($alias) {
        if (is_null($alias) || !is_string($alias)) {
            throw new InvalidArgumentException("Invalid alias string");
        }
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        $url = str_replace('{alias}', $alias, self::ALIAS_URL);
        $response = $this->request($url, null, $header, 'DELETE');
        return new DeviceResponse($response);
    }
 
    /*----Device API end----*/
 
 
    /*----Push API start----*/
    public function sendPush($data) {
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        return $this->request(self::PUSH_URL, $data, $header, 'POST');
    }
 
    public function sendValidate($data) {
        $header = array('User-Agent' => self::USER_AGENT,
            'Connection' => 'Keep-Alive',
            'Charset' => 'UTF-8',
            'Content-Type' => 'application/json');
        return $this->request(self::VALIDATE_URL, $data, $header, 'POST');
    }
    /*----Push API end----*/
 
    public function request($url, $data, $header, $method='POST') {
        $logger = JPushLog::getLogger();
        $logger->debug("Send " . $method, array(
            "method" => $method,
            "uri" => $url,
            "headers" => $header,
            "body" => $data));
 
        $request = null;
        if ($method === 'POST') {
            $request = Request::post($url);
        } else if ($method == 'DELETE') {
            $request = Request::delete($url);
        } else {
            $request = Request::get($url);
        }
 
        if (!is_null($data)) {
            $request->body($data);
        }
 
        $request->addHeaders($header)
            ->authenticateWith($this->appKey, $this->masterSecret)
            ->timeout(self::READ_TIMEOUT);
 
        $response = null;
        for ($retryTimes=0;;$retryTimes++) {
            try {
                $response = $request->send();
                break;
            } catch (ConnectionErrorException $e) {
                if (strpos($e->getMessage(),'28')) {
                    throw new APIConnectionException("Response timeout. Your request has probably be received by JPUsh Server,please check that whether need to be pushed again.", true);
                }
                if ($retryTimes >= $this->retryTimes) {
                    throw new APIConnectionException("Connect timeout. Please retry later.");
                } else {
                    $logger->debug("Retry again send " . $method, array(
                        "method" => $method,
                        "uri" => $url,
                        "headers" => $header,
                        "body" => $data,
                        "retryTimes" => $retryTimes + 1));
                }
            }
        }
        return $response;
 
    }