Getui.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace app\common\library;
  3. class Getui
  4. {
  5. protected $base_uri = "https://restapi.getui.com/v2/";
  6. private $appId;
  7. private $appKey;
  8. private $appSecret;
  9. private $masterSecret;
  10. public function __construct()
  11. {
  12. $config = config('getui');
  13. $this->appId = $config['gt_app_id'];
  14. $this->appKey = $config['gt_app_key'];
  15. $this->appSecret = $config['gt_app_secret'];
  16. $this->masterSecret = $config['gt_master_secret'];
  17. }
  18. //创建消息
  19. public function sendtoall($title, $body, $type = 1){
  20. if (!$access_token = $this->auth()) {
  21. return $this->error('ge tui token error');
  22. }
  23. // 通知
  24. $notification = [
  25. "notification"=> [
  26. "title"=> $title,
  27. "body"=> $body,
  28. "click_type"=> "url",
  29. "url"=> "https//:xxx"
  30. ],
  31. ];
  32. // 透传
  33. $transmission = [
  34. 'transmission' => json_encode([
  35. "title"=> $title,
  36. "body"=> $body,
  37. "t" => time(),
  38. ]),
  39. ];
  40. if ($type == 1) {
  41. $push_message = $notification;
  42. } else {
  43. $push_message = $transmission;
  44. }
  45. $data = [
  46. "request_id"=> createUniqueNo('PA'),
  47. "group_name"=> "",
  48. "settings"=> [
  49. "ttl"=> 7200000
  50. ],
  51. "audience"=> "all",
  52. "push_message"=> $push_message,
  53. ];
  54. $response = $this->post('/push/all', $data, [
  55. 'token:'.$access_token
  56. ]);
  57. dump($response);
  58. $data = json_decode($response, true);
  59. }
  60. /**
  61. * 消息推送
  62. * @param string $cid 设备ID
  63. * @param string $title 消息标题
  64. * @param string $body 消息内容
  65. * @param int $type 1=通知,2=透传
  66. * @param string $platform 平台
  67. * @return bool
  68. */
  69. public function push( $cid, $title, $body, $ring_name, $type = 1, $platform = 'android')
  70. {
  71. if (!$access_token = $this->auth()) {
  72. return $this->error('ge tui token error');
  73. }
  74. dump($access_token);
  75. // 通知
  76. $notification = [
  77. "notification" => [
  78. "title" => $title,
  79. "body" => $body,
  80. "ring_name" => $ring_name,
  81. "channel_level" => 4,
  82. "click_type" => "startapp",//startapp:打开应用首页,payload:自定义消息内容启动应用,
  83. "payload" => json_encode(['t' => time()])
  84. ],
  85. ];
  86. // 透传
  87. $transmission = [
  88. "transmission" => json_encode([
  89. "title" => $title,
  90. "body" => $body,
  91. "t" => time(),
  92. ], JSON_UNESCAPED_UNICODE)
  93. ];
  94. if ($type == 1) {
  95. $push_message = $notification;
  96. } else {
  97. $push_message = $transmission;
  98. }
  99. // if ($platform == 'android') {
  100. // $notify = $this->send($cid, $notification, $access_token, $push_channel ?? []);
  101. // $trans = $this->send($cid, $transmission, $access_token, $push_channel ?? []);
  102. // if (!$notify || !$trans) {
  103. // return false;
  104. // }
  105. // return true;
  106. // }
  107. if ($platform == 'ios') {
  108. $push_channel = [
  109. "ios" => [
  110. "type" => "notify",
  111. "payload" => json_encode([
  112. "title" => $title,
  113. "body" => $body,
  114. ], JSON_UNESCAPED_UNICODE),
  115. "aps" => [
  116. "alert" => [
  117. "title" => $title,
  118. "body" => $body,
  119. ],
  120. "content-available" => 0,
  121. "sound" => "com.gexin.ios.silence",
  122. "category" => "ACTIONABLE",
  123. ],
  124. "auto_badge" => "+1",
  125. ]
  126. ];
  127. }
  128. return $this->send($cid, $push_message, $access_token, $push_channel ?? []);
  129. }
  130. /**
  131. * 发送
  132. * @param $cid
  133. * @param $push_message
  134. * @param $access_token
  135. * @return bool
  136. */
  137. private function send($cid, $push_message, $access_token, $push_channel)
  138. {
  139. $params = [
  140. 'request_id' => createUniqueNo('GT'),
  141. 'settings' => [
  142. "ttl" => 7200000
  143. ],
  144. "audience" => [
  145. "cid" => [
  146. $cid
  147. ]
  148. ],
  149. "push_message" => $push_message
  150. ];
  151. if (!empty($push_channel)) {
  152. $params['push_channel'] = $push_channel;
  153. }
  154. $response = $this->post('/push/single/cid', $params, [
  155. 'token:'.$access_token
  156. ]);
  157. dump($response);
  158. /*if ($response->getStatusCode() != 200) {
  159. return $this->error($response->getReasonPhrase());
  160. }
  161. $json = $response->getBody()->getContents();
  162. $body = json_decode($json, true);
  163. return $this->success('操作成功', $body);*/
  164. }
  165. /**
  166. * 获取token
  167. * @return false|mixed
  168. */
  169. public function auth()
  170. {
  171. $timestamp = $this->ms_time();
  172. $response = $this->post('/auth', [
  173. 'sign' => hash('sha256', "{$this->appKey}{$timestamp}{$this->masterSecret}"),
  174. 'timestamp' => $timestamp,
  175. 'appkey' => $this->appKey
  176. ]);
  177. $data = json_decode($response, true);
  178. $auth = isset($data['data']['token']) ? $data['data']['token'] : '';
  179. return $auth;
  180. }
  181. private function post( $uri, $params = [], $header = [])
  182. {
  183. $common_header = ["Content-Type:application/json;charset=UTF-8","Connection: Keep-Alive"];
  184. $header = array_merge($header,$common_header);
  185. dump($header);
  186. $url = $this->base_uri.$this->appId.$uri;
  187. return curl_post($url,json_encode($params),$header);
  188. }
  189. private function ms_time()
  190. {
  191. list($ms, $sec) = explode(' ', microtime());
  192. return intval((floatval($ms) + floatval($sec)) * 1000);
  193. }
  194. }