yii框架中集成极光推送并通过别名发送推送

依据官方文档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 个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值