应该很少有人直接在极光后台使用web推送吧,容易出错也比较麻烦。最近也给自己的app写了一个后端推送功能,使用PHP语言,拿去改掉JPUSH配置信息和推送的参数就能直接使用了。
顺便说个题外话:
iOS端在配置推送的生产环境和开发环境的时候,并不是直接在注册JPUSH接口设置,而是通过证书和PP描述文件来配置的。比如使用开发证书和开发描述文件进行代码签名,这样就能够收到开发环境的推送信息。注意这里的描述文件请不要使用通配描述文件,而是使用指定具体identifier的描述文件。
代码伺候
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
<?php class Jpush { private $app_key = 'e34dfxxxxxxxxxxxxxxaeb61'; // 极光后台申请的应用appKey private $master_secret = '622ba4xxxxxxxxxxxx200f70'; // 主密码 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; } // 供外部调用的推送方法 public function send_pub($receive,$content,$parameters) { $message = ""; //存储推送状态 $result = $this->push($receive,$content,$parameters); if($result) { $res_arr = json_decode($result, true); if(isset($res_arr['error'])){ // 如果返回了error则证明失败 echo $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; } } else { $message = "发送成功!"; } } else { //接口调用失败或无响应 $message = '接口调用失败或无响应'; } return $message; } // 推送参数配置 private function push($receiver='all',$content='',$parameters='',$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'] = 'all'; // 目标用户终端手机的平台类型android,ios $data['audience'] = $receiver; // 目标用户 $data['notification'] = array( // 统一的模式--标准模式 "alert" => $content, // 安卓自定义 "android"=>array( "alert" => $content, "title" => "", "builder_id" => 1, "extras" => $parameters ), // ios的自定义 "ios" => array( "alert" => $content, "badge" => "1", "sound" => "default", "extras" => $parameters ) ); // 苹果自定义---为了弹出值方便调测 $data['message'] = array( "msg_content" => $content, "extras" => $parameters ); // 附加选项 $data['options'] = array( "sendno" => time(), "time_to_live" => $m_time, // 保存离线时间的秒数默认为一天 "apns_production" => true, // 指定 APNS 通知发送环境:false开发环境,true生产环境 ); $param = json_encode($data); $res = $this->push_curl($param,$header); if ($res) { // 得到返回值--成功已否后面判断 return $res; } else { // 未得到返回值--返回失败 return false; } } // 推送的Curl方法 private 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; } } // 验证推送参数 $pushinfos = $_POST; if(empty($pushinfos)) { $pushinfos = $_GET; } $id = $pushinfos['id']; $classid = $pushinfos['classid']; $type = $pushinfos['type']; $title = $pushinfos['title']; // 额外参数 $parameters = array( "id" => $id, "classid" => $classid, "type" => $type, ); // 发送远程推送信息 $jpush = new Jpush(); $pushResult = $jpush->send_pub('all', $title, $parameters); // 提示推送结果 echo "<script>alert('推送结果:{$pushResult}');history.go(-2);</script>"; ?> |