Yii2集成极光推送(自定义推送内容)

来源:互联网 发布:淘宝批发市场女装 编辑:程序博客网 时间:2024/06/11 09:41

1、极光文档->资源->服务器端SDK->PHP SDK下载
2、由于我使用的是YII2高级版,所以我将插件放到了vendor/yiisoft/下面,自建yii2-jpush目录,解压,将Jpush这个目录里面的文件放在该文件夹下面。
3、修改命名

将源码中的修改为 namespace yii\JPush;

http.png

Exceptions目录下面的修改为 namespace yii\JPush\Exceptions;

exec.png

4、打开extensions文件,进行配置

'yiisoft/yii2-jpush' =>   array (    'name' => 'yiisoft/yii2-jpush',    'version' => '3.5.6',    'alias' =>     array (      '@yii/JPush' => $vendorDir . '/yiisoft/yii2-jpush',    ),  ),

5、controller

define('AppKey','************');define('MasterSecret','********');use Yii;use yii\web\Response;use yii\JPush\Client;class JpushController extends \yii\web\Controller{    //put your code here    public function actionIndex(){        if(Yii::$app->request->isAjax){            $client = new Client(AppKey,MasterSecret);            $data=array(                    'act'=>'',                    'data'=>array(                            'id'=>2,                            'orderid'=>'101728225126629786',                            'poi_name'=>'湘菜龙虾湘菜龙虾',                            'poi_lat'=>'4530397.424610',                            'poi_lng'=>'12535784.223957',                    )            );            $response = $client->push()->setPlatform('all')->addAllAudience()->                            message(json_encode($data))->options(array("apns_production"=>true))->send();            if(true){                Yii::$app->response->format = Response::FORMAT_JSON;                return ['code'=>1,'message'=>'发生成功'];              }else{                Yii::$app->response->format = Response::FORMAT_JSON;                return ['code'=>2,'message'=>'发生失败'];            }      }        return $this->render('index');     }}

视图:

<script>$(document).ready(function(){             $("#btn").click(function(){                 $.get("<?php echo Url::toRoute(['jpush/index'])?>",{id:1},function(data){                     if(data.code==1){                         alert(data.message);                     }                 })             })         });</script><input value="提交" type="button" id="btn"/>
0 0
原创粉丝点击