依据官方文档https://github.com/jpush/jpush-api-php-client在项目中通过composer 安装 jpushSDK包
<?php
namespace api\modules\v1\models\services;
use JPush\Client;
use JPush\Exceptions\JPushException;
class JPushService
{
// 公共参数
public $platform = 'all';
public $alert = '';
public $content = '';
public $iosNotification = [];
public $androidNotification = [];
public $extras = [];
public $message = [];
public $options = [];
// 推送对象
public $alias = [];
public $tags = [];
private $appKey;
private $masterSecret;
private $logPath;
// 推送类型
const AUDIENCE_ALL = 1; // 全部接收者
const AUDIENCE_IOS = 2; // 全部iOS接收者
const AUDIENCE_ANDROID = 3; // 全部Android接收者
const AUDIENCE_TAG = 4; // 某标签的接收者
const AUDIENCE_ALIAS = 5; // 某别名的接收者
public function __construct()
{
}
public static function init($port = 'B')
{
$static = new self();
switch ($port) {
case 'B':
// B端
$static->appKey = \Yii::$app->params['jpush_b_app_key'];//极光后台appkey
$static->masterSecret = \Yii::$app->params['jpush_b_master_secret'];//极光后台secrect
$static->logPath = \Yii::$app->getRuntimePath().'/logs/jpush.log';//也可以不配置
break;
default :
// C端
$static->appKey = '';
$static->masterSecret = '';
$static->logPath = \Yii::$app->getRuntimePath().'/logs/jpush.log';
break;
}
return $static;
}
/**
* 推送到所有人
* @param $alert
* @param $extras
*/
public function pushAll($alert, $extras)
{
$this->alert = $alert;
$this->iosNotification = [
'category' => 'jiguang',
'extras' => $extras,
];
$this->androidNotification = [
'extras' => $extras,
];
$this->push(self::AUDIENCE_ALL);
}
/**
* 推送到iOS
* @param $alert
* @param $extras
*/
public function pushIos($alert, $extras)
{
$this->alert = $alert;
$this->iosNotification = [
'category' => 'jiguang',
'extras' => $extras,
];
$this->push(self::AUDIENCE_IOS);
}
/**
* 推送到Android
* @param $alert
* @param $extras
*/
public function pushAndroid($alert, $extras)
{
$this->alert = $alert;
$this->androidNotification = [
'extras' => $extras,
];
$this->push(self::AUDIENCE_ANDROID);
}
/**
* 推送到标签
* @param $alert
* @param $extras
* @param array $tags
*/
public function pushTag($alert, $extras, $tags = [])
{
$this->alert = $alert;
$this->iosNotification = [
'category' => 'jiguang',
'extras' => $extras,
];
$this->androidNotification = [
'extras' => $extras,
];
$this->tags = $tags;
$this->push(self::AUDIENCE_TAG);
}
/**
* 推送到别名
* @param $alert
* @param $extras
* @param string $alias
*/
public function pushAlias($alert, $extras, $alias = [])
{
$this->alert = $alert;
$this->extras = $extras;
$this->iosNotification = [
'category' => 'jiguang',
'extras' => $extras,
];
$this->androidNotification = [
'extras' => $extras,
];
$this->alias = $alias;
$this->push(self::AUDIENCE_ALIAS);
}
/**
* 推送
* @param int $audience
*/
private function push($audience = self::AUDIENCE_ALL)
{
$client = new Client($this->appKey, $this->masterSecret,$this->logPath);
$push = $client->push();
try {
// 接收平台,默认全部平台
$push->setPlatform($this->platform);
switch ($audience) {
case self::AUDIENCE_IOS :
// 接收对象,所有iOS的设备
$push->setPlatform('ios');
$push->addAllAudience();
break;
case self::AUDIENCE_ANDROID :
// 接收对象,所有Android的设备
$push->setPlatform('android');
$push->addAllAudience();
break;
case self::AUDIENCE_TAG :
// 接收对象,某个标签的设备
$push->addTag($this->tags);
break;
case self::AUDIENCE_ALIAS :
// 接收对象,某个别名设备
$push->addAlias($this->alias);
break;
default :
// 接收对象,全部设备
$push->addAllAudience();
break;
}
// 推送消息内容,所有平台相同内容
$push->setNotificationAlert($this->alert);
// 推送消息内容,iOS专用
$push->iosNotification($this->alert, $this->iosNotification);
// 推送消息内容,Android专用
$push->androidNotification($this->alert, $this->androidNotification);
// 后期可能需要$push->message
$push->send();
} catch (JPushException $e) {
\Yii::info($e->getMessage());
}
}
}
JPushService::init('B')->pushAlias('审核通过',[],[$jpushAlias]);
参数说明: ¥jpushAlias 需要推送用户的别名 注:用别名来标识一个用户 一个设备只能绑定一个别名,但多个设备可以绑定同一个别名。一次推送最多 1000 个