guanguans/yii-jpush

适配于 Yii 的极光扩展包

v1.0.5 2021-04-23 17:53 UTC

This package is auto-updated.

Last update: 2024-10-09 16:14:45 UTC


README

适配于 Yii 的极光推送扩展包

Scrutinizer Code Quality codecov StyleCI Latest Stable Version Total Downloads License

环境要求

  • 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

获取 JPush\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

License

MIT