Skip to content

Commit

Permalink
add Cid support
Browse files Browse the repository at this point in the history
  • Loading branch information
Helperhaps committed Jul 4, 2017
1 parent 5ec1a0b commit 48dc412
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

```json
"require": {
"jpush/jpush": "v3.5.*"
"jpush/jpush": "^3.5"
}
```

Expand Down
18 changes: 17 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ $push = $client->push();

通过 [JPush Push API](http://docs.jiguang.cn/server/rest_api_v3_push) 我们知道,一个 PushPayload 是由以下几个部分构成的:

- Cid
- Platform
- Audience
- Notification
- Message
- SmsContent
- Options

#### Cid

```php
$push->setCid($cid);
```

#### Platform

```php
Expand Down Expand Up @@ -208,6 +215,7 @@ $push->send();
```php
$response = $push()
->setCid('xxxxxx')
->setPlatform(['ios', 'android'])
->addTag(['tag1', 'tag2'])
->setNotificationAlert('Hello, JPush')
Expand All @@ -229,6 +237,7 @@ $response = $push()
->send();

// OR 也可以提前准备好所有的参数,然后链式调用,这样代码可读性更好一点
$cid = 'xxxxxx';
$platform = array('ios', 'android');
$alert = 'Hello JPush';
$tag = array('tag1', 'tag2');
Expand Down Expand Up @@ -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)
Expand All @@ -276,6 +286,12 @@ $response = $push->setPlatform($platform)
->send();
```

#### 获取 Cid

```php
$push->getCid($count = 1, $type = 'push');
```

## Report API

```php
Expand Down
7 changes: 7 additions & 0 deletions examples/cid_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require 'config.php';

$response = $client->push()->getCid();

print_r($response);
2 changes: 1 addition & 1 deletion examples/grouppush_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 16 additions & 0 deletions src/JPush/PushPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 48dc412

Please sign in to comment.