Push.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Master\Framework\Library\GeTui;
  4. use App\Master\Enum\RedisKeyEnum;
  5. use App\Master\Framework\Library\Library;
  6. use App\Utils\Common;
  7. use App\Utils\RedisUtil;
  8. class Push extends Library
  9. {
  10. protected string $base_uri = "https://restapi.getui.com/v2/";
  11. private string $appId;
  12. private string $appKey;
  13. private string $appSecret;
  14. private string $masterSecret;
  15. public function __construct()
  16. {
  17. $this->appId = site('gt_app_id');
  18. $this->appKey = site('gt_app_key');
  19. $this->appSecret = site('gt_app_secret');
  20. $this->masterSecret = site('gt_master_secret');
  21. }
  22. /**
  23. * 消息推送
  24. * @param string $cid 设备ID
  25. * @param string $title 消息标题
  26. * @param string $body 消息内容
  27. * @param int $type 1=通知,2=透传
  28. * @param string $platform 平台
  29. * @return bool
  30. */
  31. public function push(string $cid, string $title, string $body,string $ring_name, int $type = 1, string $platform = 'android')
  32. {
  33. if (!$access_token = $this->auth()) {
  34. return $this->error('ge tui token error');
  35. }
  36. // 通知
  37. $notification = [
  38. "notification" => [
  39. "title" => $title,
  40. "body" => $body,
  41. "ring_name" => str_replace('.mp3', '', $ring_name),
  42. "channel_level" => 4,
  43. "channel_id" => str_replace('.mp3', '', $ring_name),
  44. "channel_name" => str_replace('.mp3', '', $ring_name),
  45. "click_type" => "startapp",//startapp:打开应用首页,payload:自定义消息内容启动应用,
  46. "payload" => json_encode(['t' => time()])
  47. ],
  48. ];
  49. // 透传
  50. $transmission = [
  51. "transmission" => json_encode([
  52. "title" => $title,
  53. "body" => $body,
  54. "ring_name" => $ring_name,
  55. "t" => time(),
  56. ], JSON_UNESCAPED_UNICODE)
  57. ];
  58. switch ($type) {
  59. case 1:
  60. $push_message = $notification;
  61. break;
  62. case 2:
  63. $push_message = $transmission;
  64. break;
  65. default:
  66. $push_message = $notification;
  67. }
  68. if ($platform == 'ios') {
  69. $push_channel = [
  70. "ios" => [
  71. "type" => "notify",
  72. "payload" => json_encode([
  73. "title" => $title,
  74. "body" => $body,
  75. ], JSON_UNESCAPED_UNICODE),
  76. "aps" => [
  77. "alert" => [
  78. "title" => $title,
  79. "body" => $body,
  80. ],
  81. "content-available" => 0,
  82. "sound" => $ring_name,
  83. "category" => "ACTIONABLE",
  84. ],
  85. "auto_badge" => "+1",
  86. ]
  87. ];
  88. $push_message = $transmission;
  89. }
  90. return $this->send($cid, $push_message, $access_token, $push_channel ?? []);
  91. }
  92. /**
  93. * 发送
  94. * @param $cid
  95. * @param $push_message
  96. * @param $access_token
  97. * @return bool
  98. */
  99. private function send($cid, $push_message, $access_token, $push_channel)
  100. {
  101. $params = [
  102. 'request_id' => Common::createNo('GT', 20),
  103. 'settings' => [
  104. "ttl" => 7200000
  105. ],
  106. "audience" => [
  107. "cid" => [
  108. $cid
  109. ]
  110. ],
  111. "push_message" => $push_message
  112. ];
  113. if (!empty($push_channel)) {
  114. $params['push_channel'] = $push_channel;
  115. }
  116. $response = $this->post('/push/single/cid', $params, [
  117. 'token' => $access_token
  118. ]);
  119. if ($response->getStatusCode() != 200) {
  120. return $this->error($response->getReasonPhrase());
  121. }
  122. $json = $response->getBody()->getContents();
  123. $body = json_decode($json, true);
  124. return $this->success('操作成功', $body);
  125. }
  126. /**
  127. * 获取token
  128. * @return false|mixed
  129. */
  130. public function auth()
  131. {
  132. if ($access_token = RedisUtil::getInstance(RedisKeyEnum::UNI_PUSH_TOKEN)->get()) {
  133. return $access_token;
  134. }
  135. $timestamp = ms_time();
  136. $response = $this->post('/auth', [
  137. 'sign' => hash('sha256', "{$this->appKey}{$timestamp}{$this->masterSecret}"),
  138. 'timestamp' => $timestamp,
  139. 'appkey' => $this->appKey
  140. ]);
  141. if ($response->getStatusCode() != 200) {
  142. return false;
  143. }
  144. $json = $response->getBody()->getContents();
  145. $data = json_decode($json, true);
  146. RedisUtil::getInstance(RedisKeyEnum::UNI_PUSH_TOKEN)->setex($data['data']['token'], 7000);
  147. return $data['data']['token'];
  148. }
  149. private function post(string $uri, array $params = [], array $header = [])
  150. {
  151. return $this->postJson($this->appId.$uri,$params,$header);
  152. }
  153. }