安卓应用
作者QQ:67065435 QQ群:821635552
本站内容全部为作者原创,转载请注明出处!
安卓手机发送快应用消息(服务端代码)
<?php class QkMsgService { private $redis; private $config; public function __construct($redis) { $this->redis = $redis; $this->config = [ 'oppo' => [ 'app_id' => 99999999, 'app_key' => '99999999999999999999999999999999', 'app_secret' => '99999999999999999999999999999999', 'master_secret' => '99999999999999999999999999999999', ], 'vivo' => [ 'app_id' => 999999999, 'app_key' => '99999999999999999999999999999999', 'app_secret' => '99999999-9999-9999-9999-999999999999', ], 'xiaomi' => [ 'app_id' => '9999999999999', 'app_secret' => '9999999999999999999999==', ], 'huawei' => [ 'app_id' => 999999999, 'app_secret' => '9999999999999999999999999999999999999999999999999999999999999999', ], ]; unset($redis); } public function __destruct() { unset($this->redis, $this->config); } /** * 发送消息 * @param $request * @return array */ public function sendMsg($request) { $request['brand'] = trim((string)$request['brand']); if (isset($this->config[$request['brand']])) { //获取access_token if ($request['brand'] === 'oppo') { $access_token = $this->accessTokenOPPO($request['brand'], $this->config[$request['brand']]); } elseif ($request['brand'] === 'vivo') { $access_token = $this->accessTokenVIVO($request['brand'], $this->config[$request['brand']]); } elseif ($request['brand'] === 'xiaomi') { $access_token = $this->accessTokenXiaomi($request['brand'], $this->config[$request['brand']]); } else { $access_token = $this->accessTokenHuawei($request['brand'], $this->config[$request['brand']]); } //获取access_token失败 if (!$access_token['code']) { return ['code' => 2, 'data' => [], 'msg' => $access_token['msg']]; } else { $access_token = $access_token['access_token']; } //发送qkapp_msg if ($request['brand'] === 'oppo') { $send_res = $this->sendMsgOPPO($access_token, $request); } elseif ($request['brand'] === 'vivo') { $send_res = $this->sendMsgVIVO($access_token, $request); } elseif ($request['brand'] === 'xiaomi') { $send_res = $this->sendMsgXiaomi($access_token, $request); } else { $send_res = $this->sendMsgHuawei($access_token, $request); } //发送qkapp_msg失败 if (!$send_res['code']) { return ['code' => 3, 'data' => [], 'msg' => $send_res['msg']]; } //返回发送成功结果 return ['code' => 200, 'data' => [], 'msg' => "{$request['brand']}消息发送成功:{$send_res['msg']}"]; } else { return ['code' => 1, 'data' => [], 'msg' => "{$request['brand']}不支持推送消息"]; } } /** * OPPO获取access_token * @param $brand * @param $config * @return array */ private function accessTokenOPPO($brand, $config) { $time = time() * 1000; $redis_key = "qkapp:access_token:{$brand}"; $data = json_decode($this->redis->get($redis_key), true); if ($data['code']) { return $data; } $url = 'https://api.push.oppomobile.com/server/v1/auth'; $post = http_build_query([ 'app_key' => $config['app_key'], 'sign' => hash('sha256', "{$config['app_key']}{$time}{$config['master_secret']}"), 'timestamp' => $time, ]); $head = [ 'Content-Type: application/x-www-form-urlencoded' ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['code']) || $data['code'] !== 0) { return ['code' => false, 'access_token' => '', 'msg' => "oppo获取token:{$data['message']}"]; } else { $redis_val = ['code' => true, 'access_token' => $data['data']['auth_token']]; $this->redis->set($redis_key, json_encode($redis_val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 3600 - 100); return $redis_val; } } /** * VIVO获取access_token * @param $brand * @param $config * @return array|mixed */ private function accessTokenVIVO($brand, $config) { $time = time() * 1000; $redis_key = "qkapp:access_token:{$brand}"; $data = json_decode($this->redis->get($redis_key), true); if ($data['code']) { return $data; } $url = 'https://api-push.vivo.com.cn/message/auth '; $post = json_encode([ 'appId' => (int)$config['app_id'], 'appKey' => $config['app_key'], 'timestamp' => (int)$time, 'sign' => md5("{$config['app_id']}{$config['app_key']}{$time}{$config['app_secret']}"), ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $head = ['Content-Type: application/json']; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (empty($data['authToken'])) { return ['code' => false, 'access_token' => '', 'msg' => "vivo获取token:{$data['desc']}"]; } else { $redis_val = ['code' => true, 'access_token' => $data['authToken']]; $this->redis->set($redis_key, json_encode($redis_val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 3600 - 100); return $redis_val; } } /** * 小米获取access_token * @param $brand * @param $config * @return array */ private function accessTokenXiaomi($brand, $config) { //小米无需获取access_token return ['code' => true, 'access_token' => $config['app_secret']]; } /** * 华为获取access_token * @param $brand * @param $config * @return array|mixed */ private function accessTokenHuawei($brand, $config) { $redis_key = "qkapp:access_token:{$brand}"; $data = json_decode($this->redis->get($redis_key), true); if ($data['code']) { return $data; } $url = 'https://oauth-login.cloud.huawei.com/oauth2/v2/token'; $post = http_build_query([ 'grant_type' => 'client_credentials', 'client_id' => (int)$config['app_id'], 'client_secret' => $config['app_secret'] ]); $head = ['Content-Type: application/x-www-form-urlencoded']; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (empty($data['access_token'])) { return ['code' => false, 'access_token' => '', 'msg' => "华为获取token:{$data['msg']}"]; } else { $redis_val = ['code' => true, 'access_token' => $data['access_token']]; $this->redis->set($redis_key, json_encode($redis_val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), $data['expires_in'] - 100); return $redis_val; } } /** * OPPO发送消息 * @param $access_token * @param $request * @return array */ private function sendMsgOPPO($access_token, $request) { $request['users'] = isset($request['users']) ? trim((string)$request['users']) : ''; $request['users'] = explode('#', $request['users']); //创建消息 $url = 'https://api.push.oppomobile.com/server/v1/message/notification/save_message_content'; $post = http_build_query([ 'app_message_id' => $this->createUUID(), 'style' => 2, 'title' => $request['title'], 'sub_title' => mb_substr($request['title'], 0, 10), 'content' => mb_substr($request['description'], 0, 128), 'off_line_ttl' => 864000, //消息最多存活10天 'click_action_type' => 2,//0打开首页 1打开页面 2打开网页 4打开页面 5打开网页 'click_action_url' => "https://hapjs.org/app/{$request['app_name']}{$request['page']}", //'click_action_type' => 4,//0打开首页 1打开页面 2打开网页 4打开页面 5打开网页 //'click_action_activity' => "{$request['app_name']}", 'action_parameters' => json_encode([ 'option' => $request['option'], ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 'channel_id' => 'OPPO PUSH', ]); $head = [ 'Content-Type: application/x-www-form-urlencoded', "auth_token: {$access_token}" ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['code']) || $data['code'] !== 0) { return ['code' => false, 'msg' => "oppo创建消息:{$data['message']}"]; } else { $message_id = $data['data']['message_id']; } //推送消息 $url = 'https://api.push.oppomobile.com/server/v1/message/notification/broadcast'; if (count($request['users']) === 1 && $request['users'][0] === '') { //全量推送 $post = http_build_query([ 'message_id' => $message_id, 'target_type' => 1, ]); } else { //多人推送 $post = http_build_query([ 'message_id' => $message_id, 'target_type' => 2, 'target_value' => implode(';', $request['users']), ]); } $head = [ 'Content-Type: application/x-www-form-urlencoded', "auth_token: {$access_token}" ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['result']) || $data['result'] !== 0) { return ['code' => false, 'msg' => "oppo推送消息:{$data['message']}"]; } else { return ['code' => true, 'msg' => $data['message']]; } } /** * VIVO发送消息 * @param $access_token * @param $request * @return array */ private function sendMsgVIVO($access_token, $request) { $request['users'] = isset($request['users']) ? trim((string)$request['users']) : ''; $request['users'] = explode('#', $request['users']); if (count($request['users']) === 1 && $request['users'][0] === '') { //全量推送 $url = 'https://api-push.vivo.com.cn/message/all'; $post = json_encode([ 'notifyType' => 4, //通知 1:无 2:响铃 3:振动 4:两种 'title' => $request['title'], 'content' => $request['description'], 'timeToLive' => 604800, //60秒~7天 'skipType' => 4, //跳转 1:APP首页 2:链接 3:自定义 4:APP指定页面 'skipContent' => $request['page'], 'networkType' => -1, //网络 -1:不限 1:wifi 'clientCustomMap' => [ 'option' => $request['option'], ], 'requestId' => $this->createUUID(), ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $head = [ "Content-Type: application/json", "authToken:{$access_token}", ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['result']) || $data['result'] !== 0) { return ['code' => false, 'msg' => "vivo全量推送:{$data['desc']}"]; } else { return ['code' => true, 'msg' => $data['desc']]; } } elseif (count($request['users']) === 1) { //单人推送 $url = 'https://api-push.vivo.com.cn/message/send'; $post = json_encode([ 'regId' => (string)$request['users'][0], 'notifyType' => 4, //通知 1:无 2:响铃 3:振动 4:两种 'title' => $request['title'], 'content' => $request['description'], 'timeToLive' => 604800, //60秒~7天 'skipType' => 4, //跳转 1:APP首页 2:链接 3:自定义 4:APP指定页面 'skipContent' => $request['page'], 'networkType' => -1, //网络 -1:不限 1:wifi 'clientCustomMap' => [ 'option' => $request['option'], ], 'requestId' => $this->createUUID(), ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $head = [ "Content-Type: application/json", "authToken:{$access_token}", ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['result']) || $data['result'] !== 0) { return ['code' => false, 'msg' => "vivo单人推送:{$data['desc']}"]; } else { return ['code' => true, 'msg' => $data['desc']]; } } else { //多人推送 //1-获取taskId $url = 'https://api-push.vivo.com.cn/message/saveListPayload'; $post = json_encode([ 'title' => $request['title'], 'content' => $request['description'], 'notifyType' => 4, //通知 1:无 2:响铃 3:振动 4:两种 'timeToLive' => 604800, //60秒~7天 'skipType' => 4, //跳转 1:APP首页 2:链接 3:自定义 4:APP指定页面 'skipContent' => $request['page'], 'networkType' => -1, //网络 -1:不限 1:wifi 'clientCustomMap' => [ 'option' => $request['option'], ], 'requestId' => $this->createUUID(), ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $head = [ "Content-Type: application/json", "authToken:{$access_token}", ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['result']) || $data['result'] !== 0) { return ['code' => false, 'msg' => "vivo获取taskId:{$data['desc']}"]; } else { $taskId = $data['taskId']; } //2-批量推送用户 $url = 'https://api-push.vivo.com.cn/message/pushToList'; $post = json_encode([ 'regIds' => $request['users'], 'taskId' => $taskId, 'requestId' => $this->createUUID(), ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $head = [ "Content-Type: application/json", "authToken:{$access_token}", ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['result']) || $data['result'] !== 0) { return ['code' => false, 'msg' => "vivo多人推送:{$data['desc']}"]; } else { return ['code' => true, 'msg' => $data['desc']]; } } } /** * 小米发送消息 * @param $access_token * @param $request * @return array */ private function sendMsgXiaomi($access_token, $request) { $request['users'] = isset($request['users']) ? trim((string)$request['users']) : ''; $request['users'] = explode('#', $request['users']); if (count($request['users']) === 1 && $request['users'][0] === '') { //$url = 'https://api.xmpush.xiaomi.com/v2/message/all'; $url = 'https://api.xmpush.xiaomi.com/v2/message/topic'; $post = http_build_query([ 'description' => mb_substr($request['description'], 0, 128), 'restricted_package_name' => "{$request['app_name']}", 'title' => $request['title'], 'notify_type' => 2, 'time_to_live' => 604800000,//用户离线,消息存活14天 'pass_through' => 0, 'notify_id' => ceil(microtime(true) * 1000), 'extra.push_server_action' => 'hybrid_message', 'extra.hybrid_pn' => $request['page'], 'topic' => "{$request['app_name']}", ]); } else { $url = 'https://api.xmpush.xiaomi.com/v2/message/regid'; $post = http_build_query([ 'description' => mb_substr($request['description'], 0, 128), 'restricted_package_name' => "{$request['app_name']}", 'title' => $request['title'], 'notify_type' => 2, 'time_to_live' => 604800000,//用户离线,消息存活14天 'pass_through' => 0, 'notify_id' => ceil(microtime(true) * 1000), 'extra.push_server_action' => 'hybrid_message', 'extra.hybrid_pn' => $request['page'], 'registration_id' => implode(',', $request['users']), ]); } $head = [ "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", "Authorization: key={$access_token}", ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['result']) || $data['result'] !== 'ok') { return ['code' => false, 'msg' => "小米发送消息:{$data['description']}"]; } else { return ['code' => true, 'msg' => $data['info']]; } } /** * 华为发送消息 * @param $access_token * @param $request * @return array */ private function sendMsgHuawei($access_token, $request) { $request['users'] = isset($request['users']) ? trim((string)$request['users']) : ''; $request['users'] = explode('#', $request['users']); $url = "https://push-api.cloud.huawei.com/v1/{$this->config[$request['brand']]['app_id']}/messages:send"; $post = json_encode([ 'validate_only' => false, 'message' => [ 'data' => json_encode([ 'pushtype' => 0, 'pushbody' => [ 'title' => $request['title'], 'description' => $request['description'], 'page' => $request['page'], 'params' => [ 'option' => $request['option'], ], 'ringtone' => [ 'vibration' => 'true', 'breathLight' => 'true', ], ], ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 'android' => [ 'fast_app_target' => 2, ], 'token' => $request['users'], ], ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $head = [ "Content-type: application/json", "Authorization: Bearer {$access_token}" ]; $data = $this->curlPost($url, $post, $head); $data = (array)json_decode($data, true); if (!isset($data['code']) || ($data['code'] !== '80000000' && $data['code'] !== '80100000')) { return ['code' => false, 'msg' => "华为发送消息:{$data['msg']}"]; } else { return ['code' => true, 'msg' => $data['msg']]; } } /** * 创建唯一标识 * @return string */ private function createUUID() { if (PHP_OS == "Linux") { $uuid = trim(shell_exec("cat /proc/sys/kernel/random/uuid")); } if (empty($uuid)) { $str = md5(uniqid(mt_rand())); $uuid = ''; $uuid .= substr($str, 0, 8) . '-'; $uuid .= substr($str, 8, 4) . '-'; $uuid .= substr($str, 12, 4) . '-'; $uuid .= substr($str, 16, 4) . '-'; $uuid .= substr($str, 20, 12); } return $uuid; } /** * 创建POST请求 * @param $url * @param $post * @param $head * @return string */ private function curlPost($url, $post, $head) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_HTTPHEADER, $head); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 3); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = (string)curl_exec($ch); curl_close($ch); return $data; } }
安卓手机发送快应用消息(调用服务端代码)
<?php $vivo = [ '99999999999999999999999999999999',//regId_1 '99999999999999999999999999999999',//regId_2 ]; $oppo = [ 'CN_99999999999999999999999999999999',//regId_1 'CN_99999999999999999999999999999999',//regId_2 ]; $xiaomi = [ 'g+S799999999999999999999999999999999999999999999999999999rMB',//regId_1 'dN8k99999999999999999999999999999999999999999999999999999H5k',//regId_2 ]; $huawei = [ 'AA4Hpx99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999LinGQ',//regId_1 'AGViFu99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999cyVWQ',//regId_2 ]; $request = [ 'brand' => 'oppo', //要发送消息的平台 oppo vivo xiaomi huawei 'title' => '消息标题', //要发送消息的标题 'description' => '消息内容,消息内容,消息内容,消息内容。', //要发送消息的内容 'app_name' => 'com.baidu.quickapp', //要发送消息的应用包名 'page' => '/', //要发送消息的应用页面 'option' => 'xxxxxx', //要发送消息的携带参数 'users' => implode('#', $oppo), //要发送消息的接收用户(为空字符串则代表发送给全部用户) ]; $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $qkapp_msg = new QkMsgService($redis); $response = $qkapp_msg->sendMsg($request); $redis->close(); print_r($response);