diff --git a/src/JPush/core/DevicePayload.php b/src/JPush/core/DevicePayload.php index c78ccae..5156105 100644 --- a/src/JPush/core/DevicePayload.php +++ b/src/JPush/core/DevicePayload.php @@ -1,6 +1,7 @@ 'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); const DEVICE_URL = 'https://device.jpush.cn/v3/devices/'; const DEVICE_STATUS_URL = 'https://device.jpush.cn/v3/devices/status/'; @@ -27,17 +28,19 @@ public function getDevices($registrationId) { return $this->__processResp($response); } - public function updateDevice($registrationId, $alias = null, $addTags = null, $removeTags = null) { + public function updateDevice($registrationId, $alias = null, $mobile=null, $addTags = null, $removeTags = null) { $payload = array(); if (!is_string($registrationId)) { throw new InvalidArgumentException('Invalid registration_id'); } $aliasIsNull = is_null($alias); + $mobileIsNull = is_null($mobile); $addTagsIsNull = is_null($addTags); $removeTagsIsNull = is_null($removeTags); - if ($aliasIsNull && $addTagsIsNull && $removeTagsIsNull) { + + if ($aliasIsNull && $addTagsIsNull && $removeTagsIsNull && $mobileIsNull) { throw new InvalidArgumentException("alias, addTags, removeTags not all null"); } @@ -49,6 +52,14 @@ public function updateDevice($registrationId, $alias = null, $addTags = null, $r } } + if (!$mobileIsNull) { + if (is_string($mobile)) { + $payload['mobile'] = $mobile; + } else { + throw new InvalidArgumentException("Invalid mobile string"); + } + } + $tags = array(); if (!$addTagsIsNull) { @@ -227,10 +238,14 @@ private function __processResp($response) { $headers = $response['headers']; if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } return $body; diff --git a/src/JPush/core/PushPayload.php b/src/JPush/core/PushPayload.php index d0b4677..06d8353 100644 --- a/src/JPush/core/PushPayload.php +++ b/src/JPush/core/PushPayload.php @@ -2,6 +2,7 @@ class PushPayload { private static $EFFECTIVE_DEVICE_TYPES = array('ios', 'android', 'winphone'); + private static $LIMIT_KEYS = array('X-Rate-Limit-Limit'=>'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); const PUSH_URL = 'https://api.jpush.cn/v3/push'; const PUSH_VALIDATE_URL = ' https://api.jpush.cn/v3/push/validate'; private $client; @@ -559,13 +560,17 @@ private function __processResp($response) { $headers = $response['headers']; if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } - return (object)$body; + return $body; } else { throw new APIRequestException($response); } diff --git a/src/JPush/core/ReportPayload.php b/src/JPush/core/ReportPayload.php index b09d926..b0014a8 100644 --- a/src/JPush/core/ReportPayload.php +++ b/src/JPush/core/ReportPayload.php @@ -2,7 +2,7 @@ class ReportPayload { private static $EFFECTIVE_TIME_UNIT = array('HOUR', 'DAY', 'MONTH'); - + private static $LIMIT_KEYS = array('X-Rate-Limit-Limit'=>'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); const REPORT_URL = 'https://report.jpush.cn/v3/received'; const MESSAGES_URL = 'https://report.jpush.cn/v3/messages'; const USERS_URL = 'https://report.jpush.cn/v3/users'; @@ -81,12 +81,17 @@ private function __request($url) { $body = array(); $body['data'] = (array)json_decode($response['body']); $headers = $response['headers']; + if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } return $body; diff --git a/src/JPush/core/SchedulePayload.php b/src/JPush/core/SchedulePayload.php index d46f8e6..aceba58 100644 --- a/src/JPush/core/SchedulePayload.php +++ b/src/JPush/core/SchedulePayload.php @@ -1,6 +1,8 @@ 'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); + const SCHEDULES_URL = 'https://api.jpush.cn/v3/schedules'; private $client; @@ -171,10 +173,14 @@ private function __processResp($response) { $headers = $response['headers']; if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } return $body;