Skip to content

Commit

Permalink
update custom,用户可自定义最外层参数
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiboZhang1 committed Aug 12, 2021
1 parent 8f2b78c commit ebb191e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
54 changes: 35 additions & 19 deletions examples/push_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
use JPush\Client as JPush;

// 这里填写appKey,masterSecret以及registration_id
$app_key = 'your AppKey';
$master_secret = 'your MasterSecret';
$app_key = 'e5c0d34f58732cf09b2d4d74';
$master_secret = '4cdda6d3c8b029941dbc5cb3';
$registration_id = ('registration_id');

$client = new JPush($app_key, $master_secret);

// 简单推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!

$push_payload = $client->push()
->setPlatform('all')
->addAllAudience()
->setNotificationAlert('Hi, JPush');
try {
$response = $push_payload->send();
print_r($response);
} catch (\JPush\Exceptions\APIConnectionException $e) {
// try something here
print $e;
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
print $e;
}
// $push_payload = $client->push()
// ->setPlatform('all')
// ->addAllAudience()
// ->setNotificationAlert('Hi, JPush');
// try {
// $response = $push_payload->send();
// print_r($response);
// } catch (\JPush\Exceptions\APIConnectionException $e) {
// // try something here
// print $e;
// } catch (\JPush\Exceptions\APIRequestException $e) {
// // try something here
// print $e;
// }

// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
Expand All @@ -44,7 +44,7 @@
// ->addRegistrationId($registration_id)
->addAllAudience()

->setNotificationAlert('Hi, JPush')
->setNotificationAlert('Test custom')
->iosNotification('Hello IOS', array(
'sound' => 'sound.caf',
// 'badge' => '+1',
Expand Down Expand Up @@ -90,12 +90,12 @@
// 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
// 这里设置为 1 仅作为示例

// 'time_to_live' => 1,
'time_to_live' => 1,

// apns_production: 表示APNs是否生产环境,
// True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境

'apns_production' => false,
// 'apns_production' => false,

// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
// 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
Expand All @@ -112,6 +112,22 @@
),
'active_filter' => false
))
// custom可自定义最外层参数,如skd未支持部分文档功能,用户可自行写入
// 这里仅作为例子展示
// ->custom(array(
// 'sms_message' => array(
// 'active_filter' => false,
// 'delay_time' => 60,
// 'signid' => 154,
// 'temp_id' => 1,
// 'temp_para' => array(
// 'code' => 357
// )),
// 'options' => array(
// 'apns_production' => false,
// 'time_to_live' => 62000,
// )
// ))
->send();
print_r($response);

Expand Down
22 changes: 18 additions & 4 deletions src/JPush/PushPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PushPayload {
private $smsMessage;
private $message;
private $options;
private $custom;

/**
* PushPayload constructor.
Expand Down Expand Up @@ -311,6 +312,12 @@ public function build() {

$payload['options'] = $this->options;

if (!is_null($this->custom)) {
foreach($this->custom as $key=>$val) {
$payload[$key] = $val;
}
}

return $payload;
}

Expand Down Expand Up @@ -446,14 +453,14 @@ public function androidNotification($alert = '', array $notification = array())
* could add any custom key/value into it
*/
public function voip (array $extras = array()) {
$voipBuilder = array();
$voip = array();
if(!empty($extras)) {
foreach($extras as $key=>$val) {
$voipBuilder[$key] = $val;
$voip[$key] = $val;
}
}
$voipBuilder = array_merge($extras, $voipBuilder);
$this->voip=$voipBuilder;
$voip = array_merge($extras, $voip);
$this->voip=$voip;
return $this;
}

Expand Down Expand Up @@ -509,6 +516,13 @@ public function options(array $opts = array()) {
return $this;
}

public function custom (array $extras = array()) {
if(!empty($extras)) {
$this->custom=$extras;
}
return $this;
}

###############################################################################
############# 以下函数已过期,不推荐使用,仅作为兼容接口存在 #########################
###############################################################################
Expand Down

0 comments on commit ebb191e

Please sign in to comment.