🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 调用(thinkphp) ~~~ use lib\Jpush; ~~~ #### 通知推送 ~~~ $extras = [ 'msg' => 'OK', 'table_num' =>"1", 'table_tme' => time(), 'table_price' => "0.01" ]; $result = jPush_send_notice([$device_id], "通知消息","通知消息" $extras); echo($result); /** * 极光推送数据(发送通知消息) * @param $device_id array 设备id * @param $title string 标题 * @param $message string 消息内容 * @param $extras array 其他参数 ['type' => 1,'msg' => 'OK哦克']; */ function jPush_send_notice($device_id, $title, $message, $extras) { header("Content-type: text/html; charset=utf-8"); $appkeys = config('huiadmin.jpush.AppKey'); $masterSecret = config('huiadmin.jpush.MasterSecret'); $jpush = new jpush($masterSecret, $appkeys); $message_type = 1; $receiver = ['alias' => $device_id];//接收者 $messages = $jpush->pushMessage($title, $message, $receiver, $message_type, $extras); return $messages; } ~~~ #### 通知自定义推送 ~~~ $device_id ="931bc0d84f20bbe9"; $extras = [ 'type' => 4, 'msg' => 'OK' ]; jPush_send([$device_id], '自定义消息推送', '自定义消息推送', $extras); /** * 极光推送数据(发送自定义消息) * @param $device_id array 设备id * @param $title string 标题 * @param $message string 消息内容 * @param $extras array 其他参数 ['type' => 1,'msg' => 'OK哦克']; * @code type: 1=餐盘数据;2=下单推送给厨师;3=中奖信息(1=中奖,2=未中奖);4=结账后未清台;5=iPad餐桌登陆推送;6=厨师点击完成刷新用户订单 */ function jPush_send($device_id, $title, $message, $extras) { header("Content-type: text/html; charset=utf-8"); $appkeys = ""; $masterSecret = ""; $jpush = new jpush($masterSecret, $appkeys); $message_type = 0; $receiver = ['alias' => $device_id];//接收者 $messages = $jpush->pushMessage($title, $message, $receiver, $message_type, $extras); return $messages; } ~~~ ***** ## 类库 ~~~ <?php /** * Created by PhpStorm. * User: 投实科技 * Date: 2020-10-10 * Time: 15:13:05 * Info: 极光推送 */ namespace lib; class Jpush { private $_masterSecret = '这里是master '; private $_appkeys = '这里是key'; /** * 构造函数 * @param string $username * @param string $password * @param string $appkeys */ function __construct($masterSecret = '', $appkeys = '') { $this->_masterSecret = $masterSecret; $this->_appkeys = $appkeys; } /** * 模拟post进行url请求 * @param string $url * @param string $param */ function request_post($url = "", $param = "", $header = "") { if (empty($url) || 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; } /* $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 附加字段 //$m_time 保存离线时间的秒数默认为一天(可不传)单位为秒 //$message_type消息类型,0消息,1通知 public function pushMessage( $title = '', $message = '', $receiver = 'all', $message_type = 0, $extras = array(), $m_time = 86400, $platform = 'all' ) { $url = 'https://api.jpush.cn/v3/push'; $base64 = base64_encode("$this->_appkeys:$this->_masterSecret"); $header = array("Authorization:Basic $base64", "Content-Type:application/json"); $data = array(); $data['platform'] = $platform; //目标用户终端手机的平台类型android,ios,winphone $data['audience'] = $receiver; //目标用户 if ($message_type == 1) { $data['notification'] = array( //统一的模式--标准模式 "alert" => $message, //安卓自定义 "android" => array( "alert" => $message, "title" => $title, "builder_id" => 1, "extras" => $extras ), //ios的自定义 "ios" => array( "alert" => $message, "badge" => "1", "sound" => "default", // "extras"=>array("type"=>$m_type, "txt"=>$m_txt) ), ); } else { //苹果自定义---为了弹出值方便调测 $data['message'] = array( "title" => $title, "msg_content" => $message, "extras" => $extras ); } //附加选项 $data['options'] = array( "sendno" => time(), "time_to_live" => $m_time, //保存离线时间的秒数默认为一天 "apns_production" => 0, //指定 APNS 通知发送环境:0开发环境,1生产环境。 ); $param = json_encode($data); $res = $this->request_post($url, $param, $header); if ($res === false) { return false; } $res_arr = json_decode($res, true); if (isset($res_arr['error'])) { $messageA = $res_arr['error']['message']; //错误信息 $error_code = $res_arr['error']['code']; //错误码 switch ($error_code) { case 200: $message = '发送成功!'; break; case 1000: $message = '失败(系统内部错误)'; break; case 1001: $message = '失败(只支持 HTTP Post 方法,不支持 Get 方法)'; break; case 1002: $message = '失败(缺少了必须的参数)'; break; case 1003: $message = '失败(参数值不合法)'; break; case 1004: $message = '失败(验证失败)'; break; case 1005: $message = '失败(消息体太大)'; break; case 1008: $message = '失败(appkey参数非法)'; break; case 1020: $message = '失败(只支持 HTTPS 请求)'; break; case 1030: $message = '失败(内部服务超时)'; break; default: $message = '失败(返回其他状态,目前不清楚额,请联系开发人员!)'; break; } $message = $message."-【".$messageA."】"; $code = '201'; } else { //return $res_arr; $message = "发送成功!"; $code = '200'; } return json_encode(['msg' => $message, 'code' => $code]); } } ~~~