Getui.php 7.3 KB

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