pushNotice.php 3.8 KB

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