appId = $appId; $this->appKey = $appKey; $this->appSecret = $appSecret; $this->masterSecret = $masterSecret; $this->cacheKey = 'uni-push-'.$this->appId; } /** * 获取当前实例token * * @return void */ public function getToken() { if (Cache::has($this->cacheKey)) { return Cache::get($this->cacheKey); } else { return $this->getTokenFromServer(); } } protected function getUri($uri) { return $this->uri.$this->appId.'/'.$uri; } /** * 获取unix毫秒时间戳 * * @return void */ protected function msectime() { list($msec, $sec) = explode(' ', microtime()); $msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); return $msectime; } /** * 创建签名 * * @return void */ protected function makeSign($param) { return hash('sha256', $param['appkey'].$param['timestamp'].$this->masterSecret); } /** * 重新获取token * * @return void */ protected function getTokenFromServer() { $token = ''; $param = [ 'appkey'=>$this->appKey, 'timestamp'=>$this->msectime(), ]; $param['sign'] = $this->makeSign($param); $response = Http::withHeaders([ 'Accept' => 'application/json', ])->post($this->getUri('auth'), $param); if ($response->successful()) { $data = $response->json(); $token = $data['data']['token']; Cache::put($this->cacheKey, $data['data']['token'], Carbon::createFromTimestampMs($data['data']['expire_time'])); } return $token; } /** * 封装公共请求 * * @return void */ protected function post($uri, $params) { $token = $this->getToken(); // dump($token, $this->getUri($uri), $params); $response = Http::withHeaders([ 'Accept' => 'application/json', 'token' => $token, ])->post($this->getUri($uri), $params); $resData = []; // dd($response->json()); if ($response->successful()) { $resData = $response->json(); } return $resData; } protected function get($uri, $params = null) { $token = $this->getToken(); $response = Http::withHeaders([ 'Accept' => 'application/json', 'token' => $token, ])->get($this->getUri($uri), $params); $resData = []; if ($response->successful()) { $resData = $response->json(); } return $resData; } protected function createPushData($title, $body, $params = []) { $payload = json_encode($params); return [ 'settings'=>[ 'ttl'=> 3600000, 'strategy'=> [ 'default'=>1, 'ios'=>4, ], ], 'push_message'=>[ 'transmission'=> json_encode([ 'title' => $title, 'content'=> $body, 'payload'=> $payload, ]), // 'notification'=>[ // 'title' => $title, // 'body' => $body, // 'click_type'=> 'payload', //默认打开首页 // 'payload'=> $payload, // ], ], 'push_channel'=>[ 'android'=>[ 'ups'=> [ 'transmission'=> json_encode([ 'title' => $title, 'content'=> $body, 'payload'=> $payload, ]), // 'notification'=>[ // 'title' => $title, // 'body' => $body, // 'click_type'=> 'payload', //默认打开首页 // 'payload'=> $payload, // ], ], ], 'ios'=>[ 'type'=>'notify', 'payload' => $payload, 'aps'=>[ 'alert'=>[ 'title'=>$title, 'body'=>$body, ], 'content-available'=> 0, ], ], ], ]; } protected function createPushPayloadData($title, $body, $params = []) { $payload = json_encode($params); return [ 'settings'=>[ 'ttl'=> 3600000, 'strategy'=> [ 'default'=>1, 'ios'=>4, ], ], 'push_message'=>[ 'transmission'=> json_encode([ // 'title' => $title, // 'content'=> $body, 'payload'=> $title, ]), // 'notification'=>[ // 'title' => $title, // 'body' => $body, // 'click_type'=> 'payload', //默认打开首页 // 'payload'=> $payload, // ], ], 'push_channel'=>[ 'android'=>[ 'ups'=> [ 'transmission'=> json_encode([ // 'title' => $title, // 'content'=> $body, 'payload'=> $title, ]), // 'notification'=>[ // 'title' => $title, // 'body' => $body, // 'click_type'=> 'payload', //默认打开首页 // 'payload'=> $payload, // ], ], ], 'ios'=>[ 'type'=>'notify', 'payload' => $payload, 'aps'=>[ 'alert'=>[ 'title'=>$title, 'body'=>$body, ], 'content-available'=> 0, ], ], ], ]; } /** * 绑定cid和user_id * * @return void */ public function bindCid($userId, $cid) { $this->post('user/alias', [ 'data_list'=>[ 'cid' => $cid, 'alias'=>$userId, ], ]); } /** * 解绑所有与该别名绑定的cid */ public function unbindCid($userId) { return $this->formatResStatus( $this->get('user/alias/'.$userId) ); } /** * 根据cid单推 * * @return void */ public function pushCid(string $sn, string $cid, string $title, string $body, array $params = []) { return $this->formatResStatus( $this->post('push/single/cid', array_merge([ 'request_id' => $sn, 'audience'=>[ 'cid'=>[ $cid, ], ], ], $this->createPushData($title, $body, $params))) ); } /** * 给所有人推送 * * @param string $title * @param string $body * @param array $params * @return void */ public function pushAll(string $sn, string $title, string $body, array $params = []) { return $this->formatResStatus( $this->post('push/all', array_merge([ 'request_id' => $sn, 'audience'=>'all', ], $this->createPushData($title, $body, $params))) ); } /** * Undocumented function * * @return void */ public function pushPayload(string $sn, string $cid, string $title, string $body, array $params = []) { return $this->formatResStatus( $this->post('push/single/cid', array_merge([ 'request_id' => $sn, 'audience'=>[ 'cid'=>[ $cid, ], ], ], $this->createPushPayloadData($title, $body, $params))) ); } protected function formatResStatus($res): bool { if (Arr::get($res, 'code', 1) === 0) { return true; } return false; } }