pushNotice.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace common\components;
  3. use GTClient;
  4. use GTNotification;
  5. use GTPushMessage;
  6. use GTPushRequest;
  7. use GuzzleHttp\Client;
  8. use GuzzleHttp\Exception\GuzzleException;
  9. use Yii;
  10. class pushNotice
  11. {
  12. const domainUrl = 'https://restapi.getui.com';
  13. const appKey = 'uC5ZdrWSV48cLRBJWMSId9';
  14. const appId = 'YjALucIHmI7DylcjlMZEx2';
  15. const masterSecret = 'zSbdBLOQrxAa5IcDNqIne7';
  16. //版本2:个推开放平台接口前缀(BaseUrl): https://restapi.getui.com/v2/$appId
  17. const baseUrl = 'https://restapi.getui.com/v2';
  18. public static function push($adminId, $cId, $title, $body)
  19. {
  20. $notify = new \GTNotification();
  21. $notify->setTitle($title);
  22. $notify->setBody($body);
  23. //点击通知后续动作,目前支持以下后续动作:
  24. //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
  25. $notify->setClickType("startapp");
  26. //保证有振动
  27. $notify->setChannelId('channelId1');
  28. $notify->setChannelName('channelName1');
  29. $notify->setChannelLevel(4);
  30. $message = new \GTPushMessage();
  31. $message->setNotification($notify);
  32. //设置推送参数
  33. $push = new \GTPushRequest();
  34. $time = time();
  35. $rand = rand(1000, 9999);
  36. $rId = $time . $rand . $adminId;
  37. $push->setRequestId($rId);
  38. $push->setPushMessage($message);
  39. $push->setCid($cId);
  40. //创建API,APPID等配置参考 环境要求 进行获取
  41. $api = new \GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
  42. $api->pushApi()->pushToSingleByCid($push);
  43. }
  44. public static function nPush($adminId, $cId, $title, $body)
  45. {
  46. //设置推送参数
  47. $time = time();
  48. $rand = rand(1000, 9999);
  49. $rId = $time . $rand . $adminId;
  50. $push = new GTPushRequest();
  51. $push->setRequestId($rId);
  52. $message = new GTPushMessage();
  53. $notify = new GTNotification();
  54. $notify->setTitle($title);
  55. $notify->setBody($body);
  56. //点击通知后续动作,目前支持以下后续动作:
  57. //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
  58. $notify->setClickType("startapp");
  59. $message->setNotification($notify);
  60. $push->setPushMessage($message);
  61. $push->setCid($cId);
  62. //创建API,APPID等配置参考 环境要求 进行获取
  63. $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
  64. //处理返回结果
  65. $result = $api->pushApi()->pushToSingleByCid($push);
  66. Yii::info('push/single/cid: ' . json_encode($result));
  67. }
  68. // 执行cid单推
  69. public static function newPush($cId, $title, $body)
  70. {
  71. $bodyArr['title'] = $title;
  72. $bodyArr['body'] = $body;
  73. $bodyArr['click_type'] = 'startapp';
  74. $token = Yii::$app->redis->executeCommand('GET', ['getui_token']);
  75. if (empty($token)) {
  76. $token = self::getToken();
  77. if ($token == "") {
  78. Yii::error('generate getui token fail');
  79. return;
  80. }
  81. Yii::$app->redis->executeCommand('SET', ['getui_token', $token, 'EX', 86400]);
  82. }
  83. $client = new Client([
  84. 'base_uri' => self::baseUrl,
  85. 'timeout' => 5.0,
  86. ]);
  87. try {
  88. $response = $client->request('POST', self::baseUrl . '/push/single/cid', [
  89. 'form_params' => [
  90. 'request_id' => time(),
  91. 'settings' => ['ttl' => 86400000],
  92. 'audience' => [
  93. 'cid' => $cId,
  94. ],
  95. 'push_message' => [
  96. 'notification' => $bodyArr
  97. ],
  98. 'headers' => [
  99. 'token' => $token
  100. ]
  101. ]
  102. ]);
  103. } catch (GuzzleException $e) {
  104. Yii::error('push/single/cid: ' . $e->getMessage());
  105. }
  106. // 失败,则生重新获取 token ,再发送
  107. }
  108. public static function getToken()
  109. {
  110. $i = 0;
  111. do{
  112. $client = new Client([
  113. 'base_uri' => self::baseUrl,
  114. 'timeout' => 5.0
  115. ]);
  116. $response = $client->request('POST', self::baseUrl . '/auth', [
  117. 'form_params' => [
  118. "sign" => hash('sha256', self::appKey . time() . self::masterSecret),
  119. "timestamp" => round(microtime(true) * 1000),
  120. "appkey" => self::appKey
  121. ]
  122. ]);
  123. $data = json_decode($response, true);
  124. }while(!isset($data['data']['token']) && $i++ < 3);
  125. if ($i > 3) {
  126. return "";
  127. }
  128. return $data['data']['token'];
  129. }
  130. }