From 48dc412eb7a5e9a56238a9a91e2dbabb7c2f99fd Mon Sep 17 00:00:00 2001 From: Helperhaps Date: Mon, 3 Jul 2017 10:47:58 +0000 Subject: [PATCH] add Cid support --- README.md | 2 +- doc/api.md | 18 +++++++++++++++++- examples/cid_example.php | 7 +++++++ examples/grouppush_example.php | 2 +- src/JPush/PushPayload.php | 16 ++++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 examples/cid_example.php diff --git a/README.md b/README.md index dddd88a..c6f01e9 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ ```json "require": { - "jpush/jpush": "v3.5.*" + "jpush/jpush": "^3.5" } ``` diff --git a/doc/api.md b/doc/api.md index 8b5bd55..29d848a 100644 --- a/doc/api.md +++ b/doc/api.md @@ -34,6 +34,7 @@ $push = $client->push(); 通过 [JPush Push API](http://docs.jiguang.cn/server/rest_api_v3_push) 我们知道,一个 PushPayload 是由以下几个部分构成的: +- Cid - Platform - Audience - Notification @@ -41,6 +42,12 @@ $push = $client->push(); - SmsContent - Options +#### Cid + +```php +$push->setCid($cid); +``` + #### Platform ```php @@ -208,6 +215,7 @@ $push->send(); ```php $response = $push() + ->setCid('xxxxxx') ->setPlatform(['ios', 'android']) ->addTag(['tag1', 'tag2']) ->setNotificationAlert('Hello, JPush') @@ -229,6 +237,7 @@ $response = $push() ->send(); // OR 也可以提前准备好所有的参数,然后链式调用,这样代码可读性更好一点 +$cid = 'xxxxxx'; $platform = array('ios', 'android'); $alert = 'Hello JPush'; $tag = array('tag1', 'tag2'); @@ -266,7 +275,8 @@ $options = array( 'override_msg_id' => 100, 'big_push_duration' => 100 ); -$response = $push->setPlatform($platform) +$response = $push->setCid($cid) + ->setPlatform($platform) ->addTag($tag) ->addRegistrationId($regId) ->iosNotification($alert, $ios_notification) @@ -276,6 +286,12 @@ $response = $push->setPlatform($platform) ->send(); ``` +#### 获取 Cid + +```php +$push->getCid($count = 1, $type = 'push'); +``` + ## Report API ```php diff --git a/examples/cid_example.php b/examples/cid_example.php new file mode 100644 index 0000000..97b6aee --- /dev/null +++ b/examples/cid_example.php @@ -0,0 +1,7 @@ +push()->getCid(); + +print_r($response); diff --git a/examples/grouppush_example.php b/examples/grouppush_example.php index ec63d86..4cbe45a 100644 --- a/examples/grouppush_example.php +++ b/examples/grouppush_example.php @@ -6,7 +6,7 @@ $group_key = 'xxxx'; $group_master_secret = 'xxxx'; -$client = new JPush('group-' . $group_key, $group_master_secret); +$client = new JPush('group-' . $group_key, $group_master_secret, null); $push_payload = $client->push() ->setPlatform('all') diff --git a/src/JPush/PushPayload.php b/src/JPush/PushPayload.php index dc8821d..a2af2a8 100644 --- a/src/JPush/PushPayload.php +++ b/src/JPush/PushPayload.php @@ -8,9 +8,12 @@ class PushPayload { const PUSH_URL = 'https://api.jpush.cn/v3/push'; const GROUP_PUSH_URL = 'https://api.jpush.cn/v3/grouppush'; const PUSH_VALIDATE_URL = 'https://api.jpush.cn/v3/push/validate'; + const CID_URL = 'https://api.jpush.cn/v3/push/cid'; private $client; private $url; + + private $cid; private $platform; private $audience; @@ -39,6 +42,15 @@ function __construct($client) { $this->url = $this->client->is_group() ? PushPayload::GROUP_PUSH_URL : PushPayload::PUSH_URL; } + public function getCid($count = 1, $type = 'push') { + $url = self::CID_URL . '?count=' . $count . '&type =' . $type; + return Http::get($this->client, $url); + } + + public function setCid($cid) { + $this->cid = trim($cid); + } + public function setPlatform($platform) { # $required_keys = array('all', 'android', 'ios', 'winphone'); if (is_string($platform)) { @@ -198,6 +210,10 @@ public function build() { } $payload["platform"] = $this->platform; + if (!is_null($this->cid)) { + $payload['cid'] = $this->cid; + } + // validate audience $audience = array(); if (!is_null($this->tags)) {