适配于 Yii 的极光推送扩展包
- yii >= 2
$ composer require guanguans/yii-jpush --prefer-dist -v
Yii2 配置文件 config/main.php
的 components 中添加:
'components' => [
// ...
'jpush' => [
'class' => 'Guanguans\YiiJpush\Jpush',
'appKey' => 'xxxxxxxxxxx',
'masterSecret' => 'xxxxxxxxxxx',
'logFile' => './jpush.log', // 可选
'retryTimes' => 3, // 可选
'zone' => 'default', // 可选 [default, bj]
],
// ...
]
使用,更多详细文档请参考 jpush/jpush-api-php-client
<php
Yii::$app->jpush->client
<?php
Yii::$app->jpush->client->push()
->setPlatform('all')
->addAllAudience()
->setNotificationAlert('Hello, JPush')
->send();
/**
* @param string $content 推送内容
* @param array $ids 推送的id
* @param string $info 业务内容
* @param string $title 推送标题 定向的简单推送 不填
*/
Yii::$app->jpush->client->push()
->setPlatform(['ios', 'android'])
->addRegistrationId($ids)
->iosNotification([
"title" => $title,
"body" => $content
], [
'sound' => 'sound.caf',
'badge' => '+1',
'content-available' => true,
'mutable-content' => true,
'category' => 'jiguang',
'extras' => [
'info' => $info,
],
])
->androidNotification($content, [
'title' => $title,
'extras' => [
'info' => $info,
],
])
->options([
// true 表示推送生产环境,false 表示要推送开发环境;如果不指定则默认为推送开发环境
'apns_production' => false,
])
->send();
<?php
$pusher = Yii::$app->jpush->client->push();
$pusher->setPlatform('all');
$pusher->addAllAudience();
$pusher->setNotificationAlert('Hello, JPush');
try {
$pusher->send();
} catch (\Exception $e) {
// try something else here
echo $e;
}
$ composer test