appId = site('gt_app_id'); $this->appKey = site('gt_app_key'); $this->appSecret = site('gt_app_secret'); $this->masterSecret = site('gt_master_secret'); } /** * 消息推送 * @param string $cid 设备ID * @param string $title 消息标题 * @param string $body 消息内容 * @param int $type 1=通知,2=透传 * @param string $platform 平台 * @return bool */ public function push(string $cid, string $title, string $body,string $ring_name, int $type = 1, string $platform = 'android') { if (!$access_token = $this->auth()) { return $this->error('ge tui token error'); } // 通知 $notification = [ "notification" => [ "title" => $title, "body" => $body, "ring_name" => str_replace('.mp3', '', $ring_name), "channel_level" => 4, "channel_id" => str_replace('.mp3', '', $ring_name), "channel_name" => str_replace('.mp3', '', $ring_name), "click_type" => "startapp",//startapp:打开应用首页,payload:自定义消息内容启动应用, "payload" => json_encode(['t' => time()]) ], ]; // 透传 $transmission = [ "transmission" => json_encode([ "title" => $title, "body" => $body, "ring_name" => $ring_name, "t" => time(), ], JSON_UNESCAPED_UNICODE) ]; switch ($type) { case 1: $push_message = $notification; break; case 2: $push_message = $transmission; break; default: $push_message = $notification; } if ($platform == 'ios') { $push_channel = [ "ios" => [ "type" => "notify", "payload" => json_encode([ "title" => $title, "body" => $body, ], JSON_UNESCAPED_UNICODE), "aps" => [ "alert" => [ "title" => $title, "body" => $body, ], "content-available" => 0, "sound" => $ring_name, "category" => "ACTIONABLE", ], "auto_badge" => "+1", ] ]; $push_message = $transmission; } return $this->send($cid, $push_message, $access_token, $push_channel ?? []); } /** * 发送 * @param $cid * @param $push_message * @param $access_token * @return bool */ private function send($cid, $push_message, $access_token, $push_channel) { $params = [ 'request_id' => Common::createNo('GT', 20), 'settings' => [ "ttl" => 7200000 ], "audience" => [ "cid" => [ $cid ] ], "push_message" => $push_message ]; if (!empty($push_channel)) { $params['push_channel'] = $push_channel; } $response = $this->post('/push/single/cid', $params, [ 'token' => $access_token ]); if ($response->getStatusCode() != 200) { return $this->error($response->getReasonPhrase()); } $json = $response->getBody()->getContents(); $body = json_decode($json, true); return $this->success('操作成功', $body); } /** * 获取token * @return false|mixed */ public function auth() { if ($access_token = RedisUtil::getInstance(RedisKeyEnum::UNI_PUSH_TOKEN)->get()) { return $access_token; } $timestamp = ms_time(); $response = $this->post('/auth', [ 'sign' => hash('sha256', "{$this->appKey}{$timestamp}{$this->masterSecret}"), 'timestamp' => $timestamp, 'appkey' => $this->appKey ]); if ($response->getStatusCode() != 200) { return false; } $json = $response->getBody()->getContents(); $data = json_decode($json, true); RedisUtil::getInstance(RedisKeyEnum::UNI_PUSH_TOKEN)->setex($data['data']['token'], 7000); return $data['data']['token']; } private function post(string $uri, array $params = [], array $header = []) { return $this->postJson($this->appId.$uri,$params,$header); } }