声明:项目本身是在win下面开发的,所以没有用到composer。
1、极光文档->资源->服务器端SDK->PHP SDK下载
2、由于我使用的是YII2高级版,所以我将插件放到了vendor/yiisoft/下面,自建yii2-jpush目录,解压,将Jpush这个目录里面的文件放在该文件夹下面。
3、修改命名
将源码中的修改为 namespace yii\JPush;
Exceptions目录下面的修改为 namespace yii\JPush\Exceptions;
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"/>