// 极光的key和secret,在极光注册完应用后,会生成
protected function _initialize()
{
parent::_initialize();
$jpushConfig = Config::get("jpush");
$this->app_key = $jpushConfig['app_key'];
$this->master_secret = $jpushConfig['master_secret'];
$this->url = "https://api.jpush.cn/v3/push";//推送的地址
$this->userModel = model('User');
//用户信息
if (Session::get('user.share_user_id')) {
$this->user_id = Session::get('user.share_user_id');
$this->company_id = Session::get('user.share_com_id');
} else {
$this->user_id = Session::get('user.id');
$userInfo = $this->userModel->where(['id' => $this->user_id])->find();
$this->company_id = $userInfo['company_id'];
if ($this->company_id != Session::get('user.company_id'))
Session::set('user.company_id', $this->company_id);
}
}
public function sendMsg($type,$pushData){
//需要为设备设置aliassendMsg
if($type=='all'){
$receiver = 'all';
}else{
$alias = $type;
$receiver['alias'] = array($alias);
}
$content = $pushData['content'];
$title = $pushData['title'];
$extras = array();
$result = $this->push($receiver,$content,$title,$extras);
if($result){
$res_arr = json_decode($result, true);
if(isset($res_arr['error'])){ //如果返回了error则证明失败
//错误信息 错误码
echo json_encode(array('status'=>0,'errorCode'=>$res_arr['error']['code'],'errorMessage'=>$res_arr['error']['message']));
exit;
}else{
//处理成功的推送......
//可执行一系列对应操作~
echo json_encode(array('status'=>1,'msg'=>'推送成功'));
exit;
}
}else{ //接口调用失败或无响应
echo json_encode(array('status'=>0,'msg'=>'接口调用失败或无响应'));
exit;
}
}
public function push($receiver = 'all', $content = '', $title = '', $extras = array(), $m_time = 86400)
{
$app_key = $this->app_key; //待发送的应用程序(appKey),只能填一个。
$master_secret = $this->master_secret; //主密码
$url = "https://api.jpush.cn/v3/push"; //推送的地址
$base64 = base64_encode("{$app_key}:{$master_secret}");
$header = array("Authorization:Basic {$base64}", "Content-Type:application/json");
$data = array();
$data['platform'] = 'all'; //目标用户终端手机的平台类型android,ios,winphone
$data['audience'] = $receiver; //目标用户
$data['notification'] = array(
//统一的模式--标准模式
"alert" => $content,
//安卓自定义
"android" => array(
"alert" => $content,
"title" => $title,
"builder_id" => 1,
"extras" => array("data" => $extras)
),
//ios的自定义
"ios" => array(
"alert" => $content,
"badge" => "1",
"sound" => "default",
"content-available" => true,
"extras" => array("data" => $extras)
),
);
//附加选项
$data['options'] = array(
"sendno" => time(),
"time_to_live" => $m_time, //保存离线时间的秒数默认为一天
"apns_production" => false, //指定 APNS 通知发送环境:0开发环境,1生产环境。
);
$param = json_encode($data);
$res = $this->push_curl($param, $header, $url);
if ($res) { //得到返回值--成功已否后面判断
return $res;
} else { //未得到返回值--返回失败
return false;
}
}
//推送的Curl方法
public function push_curl($param = "", $header = "", $url = "")
{
if (empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // 增加 HTTP Header(头)里的字段
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
步骤:
Demo 用法
通过 Android Studio 引入项目目录下的 android 目录,再替换 ./android/app/build.gradle 中的「应用的包名」和「应用的 AppKey」。
集成指南
HBuilder 项目集成第三方插件,需先参考 HBuilder 官方的离线打包教程,将您的 HBuilder 项目集成进 Android 工程中。之后再执行以下步骤:
1.拷贝 ./android/app/src/main/java/io.dcloud.feature.jpush 文件夹至你 Android Studio 工程的 /src/main/java/ 目录下。
2.拷贝 ./jpush.js 到你 Android Studio 工程的 /assets/apps/HBuilder应用名/js/ 下。
3.在 /assets/apps/你的应用名/www/manifest.json 文件中添加:
4.在 /assets/data/dcloud_properties.xml 中添加(如果已存在 Push feature,可以直接修改):
5.在 app/build.gradle 中添加:
6.在 AndroidManifest.xml 中添加:
错误: app:transformClassesWithMultidexlistForDebug
参考:https://www.edwardbeazer.com/execution-failed-for-task-app-transformclasseswithmultidexlistfordebug/
androidStudio下集成hbuild推送报错