您的位置:首页 > 移动开发

thinkphp+APP 消息推送

2017-06-14 16:02 176 查看

极光推送

一、申请极光账号:https://www.jiguang.cn
二、进入控制台(极光开发者服务),创建应用;创建成功后得到AppKey和Master
Secret两个参数(后面代码要用);



三、让app开发人员用这个账号在这个平台下载对应的sdk做相应的处理就OK了

php代码内容

一、将下面的类放入到目录ThinkPHP/Library/Vendor/JPush下面

<?php
//文件名路劲   Vendor/JPush/JPush.php
class Jpush{

private $app_key = 'b*******************3';            //待发送的应用程序(appKey),只能填一个。
private $master_secret = '2******************da';      //主密码
private $url = "https://api.jpush.cn/v3/push";            //推送的地址

//若实例化的时候传入相应的值则按新的相应值进行
public function __construct($app_key=null, $master_secret=null,$url=null) {
if ($app_key) $this->app_key = $app_key;
if ($master_secret) $this->master_secret = $master_secret;
if ($url) $this->url = $url;
}

/*  $receiver 接收者的信息
all 字符串 该产品下面的所有用户. 对app_key下的所有用户推送消息
tag(20个)Array标签组(并集): tag=>array('昆明','北京','曲靖','上海');
tag_and(20个)Array标签组(交集): tag_and=>array('广州','女');
alias(1000)Array别名(并集): alias=>array('93d78b73611d886a74*****88497f501','606d05090896228f66ae10d1*****310');
registration_id(1000)注册ID设备标识(并集): registration_id=>array('20effc071de0b45c1a**********2824746e1ff2001bd80308a467d800bed39e');

$content 推送的内容。
$extras  附加字段  array类型
$m_time 保存离线时间的秒数默认为一天(可不传)单位为秒
*/
public function push($receiver='all', $title='', $content='', $extras, $m_time='86400'){
$base64=base64_encode("$this->app_key:$this->master_secret");
$header=array("Authorization:Basic $base64","Content-Type:application/json");
$data = array();
$data['platform'] = 'android';          //目标用户终端手机的平台类型android,ios,winphone
$data['audience'] = $receiver;          //目标用户
//发送通知
$data['notification'] = array(
//统一的模式--标准模式
"alert"=>$content,

);

//自定义信息
$data['message'] = array(
"msg_content"=>$content,
"extras"=>$extras
);

//附加选项
$data['options'] = array(
"sendno"=>time(),
"time_to_live"=>$m_time,      //保存离线时间的秒数默认为一天
"apns_production"=>1,        //指定 APNS 通知发送环境:0开发环境,1生产环境。
);
$param = json_encode($data);
$res = $this->push_curl($param,$header);

if($res){       //得到返回值--成功已否后面判断
return $res;
}else{          //未得到返回值--返回失败
return false;
}
}

//推送的Curl方法
public function push_curl($param="",$header="") {
if (empty($param)) { return false; }
$postUrl = $this->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;
}
}

?>


二、在Common中的function.php中添加下面方法

function app_send($receive,$title,$content,$extras,$m_time){
Vendor('JPush.JPush');
$pushObj = new \Jpush();
//调用推送,并处理
$result = $pushObj->push($receive,$title,$content,$extras,$m_time);
if($result){
$res_arr = json_decode($result, true);
if(isset($res_arr['error'])){   //如果返回了error则证明失败
//错误信息 错误码
$data['code'] = 201;
$data['msg'] = $res_arr['error']['message'].':'.$res_arr['error']['code'];
return json_encode($data);
}else{
//处理成功的推送......
//可执行一系列对应操作~
$data['code'] = 200;
$data['msg'] = '推送成功';
return json_encode($data);
}
}else{      //接口调用失败或无响应
$data['code'] = 201;
$data['msg'] = '接口调用失败或无响应~';
return json_encode($data);
}
}

三、控制器调用

public function test(){
//组装需要的参数
//$receive = 'all';     //全部
//$receive = array('tag'=>array('1','2','3'));      //标签
$receive = array('alias'=>array('117'));    //别名 id推送
$title = '标题';
$content = '你的订单任务已发布成功,请耐心等候..';
$m_time = '86400';        //离线保留时间
$extras = array("versionname"=>'123', "versioncode"=>'456');   //自定义数组
echo app_send($receive,$title,$content,$extras,$m_time);
}

四、完工

如需转载请备注原创地址:http://blog.csdn.net/weixin_38332043
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp php 服务器 app sdk