123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- declare(strict_types=1);
- namespace App\Master\Framework\Library\GeTui;
- use App\Master\Enum\RedisKeyEnum;
- use App\Master\Framework\Library\Library;
- use App\Utils\Common;
- use App\Utils\RedisUtil;
- class Push extends Library
- {
- protected string $base_uri = "https://restapi.getui.com/v2/";
- private string $appId;
- private string $appKey;
- private string $appSecret;
- private string $masterSecret;
- public function __construct()
- {
- $this->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);
- }
- }
|