pushNotice.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace common\components;
  3. use GTAlert;
  4. use GTAndroid;
  5. use GTAps;
  6. use GTClient;
  7. use GTIos;
  8. use GTMultimedia;
  9. use GTNotification;
  10. use GTPushChannel;
  11. use GTPushMessage;
  12. use GTPushRequest;
  13. use GTSettings;
  14. use GTStrategy;
  15. use GTThirdNotification;
  16. use GTUps;
  17. use GuzzleHttp\Client;
  18. use GuzzleHttp\Exception\GuzzleException;
  19. use Yii;
  20. class pushNotice
  21. {
  22. const domainUrl = 'https://restapi.getui.com';
  23. const appKey = 'uC5ZdrWSV48cLRBJWMSId9';
  24. const appId = 'YjALucIHmI7DylcjlMZEx2';
  25. const masterSecret = 'zSbdBLOQrxAa5IcDNqIne7';
  26. //版本2:个推开放平台接口前缀(BaseUrl): https://restapi.getui.com/v2/$appId
  27. const baseUrl = 'https://restapi.getui.com/v2';
  28. public static function push($adminId, $cId, $title, $body)
  29. {
  30. $notify = new GTNotification();
  31. $notify->setTitle($title);
  32. $notify->setBody($body);
  33. //点击通知后续动作,目前支持以下后续动作:
  34. //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
  35. $notify->setClickType("startapp");
  36. //保证有振动
  37. $notify->setChannelId('channelId1');
  38. $notify->setChannelName('channelName1');
  39. $notify->setChannelLevel(4);
  40. $message = new GTPushMessage();
  41. $message->setNotification($notify);
  42. //设置推送参数
  43. $push = new GTPushRequest();
  44. $time = time();
  45. $rand = rand(1000, 9999);
  46. $rId = $time . $rand . $adminId;
  47. $push->setRequestId($rId);
  48. $push->setPushMessage($message);
  49. $push->setCid($cId);
  50. //创建API,APPID等配置参考 环境要求 进行获取
  51. $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
  52. $api->pushApi()->pushToSingleByCid($push);
  53. }
  54. public static function nPush($adminId, $cId, $title, $body)
  55. {
  56. Yii::info('start -- push/single/cid');
  57. $set = new GTSettings();
  58. $set->setTtl(3600000);
  59. $strategy = new GTStrategy();
  60. $strategy->setDefault(GTStrategy::STRATEGY_THIRD_FIRST);
  61. // $strategy->setIos(GTStrategy::STRATEGY_GT_ONLY);
  62. // $strategy->setOp(GTStrategy::STRATEGY_THIRD_FIRST);
  63. // $strategy->setHw(GTStrategy::STRATEGY_THIRD_ONLY);
  64. $strategy->setHw(GTStrategy::STRATEGY_GT_FIRST);
  65. $set->setStrategy($strategy);
  66. //设置推送参数
  67. $time = time();
  68. $rand = rand(1000, 9999);
  69. $rId = $time . $rand . $adminId;
  70. $push = new GTPushRequest();
  71. $push->setRequestId(self::micro_time());
  72. $push->setSettings($set);
  73. $message = new GTPushMessage();
  74. $notify = new GTNotification();
  75. $notify->setTitle($title);
  76. $notify->setBody($body);
  77. $notify->setChannelId('channelId1');
  78. $notify->setChannelName('channelName1');
  79. $notify->setChannelLevel(4);
  80. //点击通知后续动作,目前支持以下后续动作:
  81. //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
  82. $notify->setClickType("startapp");
  83. $message->setNotification($notify);
  84. $push->setPushMessage($message);
  85. $push->setCid($cId);
  86. //厂商推送消息参数
  87. $pushChannel = new GTPushChannel();
  88. // ------------- ios ---------------
  89. $ios = new GTIos();
  90. $ios->setType("notify");
  91. $ios->setAutoBadge("1");
  92. $ios->setPayload("ios_payload");
  93. $ios->setApnsCollapseId("apnsCollapseId");
  94. //aps设置
  95. $aps = new GTAps();
  96. $aps->setContentAvailable(0);
  97. $aps->setSound("com.gexin.ios.silenc");
  98. $aps->setCategory("category");
  99. $aps->setThreadId("threadId");
  100. $alert = new GTAlert();
  101. $alert->setTitle($title);
  102. $alert->setBody($body);
  103. $alert->setActionLocKey("ActionLocKey");
  104. $alert->setLocKey("LocKey");
  105. $alert->setLocArgs(array("LocArgs1","LocArgs2"));
  106. $alert->setLaunchImage("LaunchImage");
  107. $alert->setTitleLocKey("TitleLocKey");
  108. $alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2"));
  109. $alert->setSubtitle("Subtitle");
  110. $alert->setSubtitleLocKey("SubtitleLocKey");
  111. $alert->setSubtitleLocArgs(array("subtitleLocArgs1","subtitleLocArgs2"));
  112. $aps->setAlert($alert);
  113. $ios->setAps($aps);
  114. /*$multimedia = new GTMultimedia();
  115. $multimedia->setUrl("url");
  116. $multimedia->setType(1);
  117. $multimedia->setOnlyWifi(false);
  118. $multimedia2 = new GTMultimedia();
  119. $multimedia2->setUrl("url2");
  120. $multimedia2->setType(2);
  121. $multimedia2->setOnlyWifi(true);
  122. $ios->setMultimedia(array($multimedia));
  123. $ios->addMultimedia($multimedia2);*/
  124. $pushChannel->setIos($ios);
  125. // ------------- ios end ---------------
  126. // ----------------- 安卓 -----------------
  127. $android = new GTAndroid();
  128. $ups = new GTUps();
  129. //$ups->setTransmission("ups Transmission");
  130. $ups->addOption("HW","badgeAddNum",1);
  131. //$ups->addOption("OP","channel","Default");
  132. //$ups->addOption("OP","aaa","bbb");
  133. //$ups->addOption(null,"a","b");
  134. $thirdNotification = new GTThirdNotification();
  135. $thirdNotification->setTitle($title);
  136. $thirdNotification->setBody("body-".self::micro_time().': ' . $body);
  137. $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_STAERAPP);
  138. //$thirdNotification->setIntent("intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end");
  139. //$thirdNotification->setUrl("http://docs.getui.com/getui/server/rest_v2/push/");
  140. //$thirdNotification->setPayload("payload");
  141. $thirdNotification->setNotifyId(self::micro_time());
  142. $ups->setNotification($thirdNotification);
  143. $android->setUps($ups);
  144. $pushChannel->setAndroid($android);
  145. // ----------------- 安卓 end -----------------
  146. $push->setPushChannel($pushChannel);
  147. //创建API,APPID等配置参考 环境要求 进行获取
  148. $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
  149. //处理返回结果
  150. $result = $api->pushApi()->pushToSingleByCid($push);
  151. Yii::info('push/single/cid: ' . json_encode($result));
  152. }
  153. public static function micro_time()
  154. {
  155. list($usec, $sec) = explode(" ", microtime());
  156. $time = ($sec . substr($usec, 2, 3));
  157. return $time;
  158. }
  159. // 执行cid单推
  160. public static function newPush($cId, $title, $body)
  161. {
  162. $bodyArr['title'] = $title;
  163. $bodyArr['body'] = $body;
  164. $bodyArr['click_type'] = 'startapp';
  165. $token = Yii::$app->redis->executeCommand('GET', ['getui_token']);
  166. if (empty($token)) {
  167. $token = self::getToken();
  168. if ($token == "") {
  169. Yii::error('generate getui token fail');
  170. return;
  171. }
  172. Yii::$app->redis->executeCommand('SET', ['getui_token', $token, 'EX', 86400]);
  173. }
  174. $client = new Client([
  175. 'base_uri' => self::baseUrl,
  176. 'timeout' => 5.0,
  177. ]);
  178. try {
  179. $response = $client->request('POST', self::baseUrl . '/push/single/cid', [
  180. 'form_params' => [
  181. 'request_id' => time(),
  182. 'settings' => ['ttl' => 86400000],
  183. 'audience' => [
  184. 'cid' => $cId,
  185. ],
  186. 'push_message' => [
  187. 'notification' => $bodyArr
  188. ],
  189. 'headers' => [
  190. 'token' => $token
  191. ]
  192. ]
  193. ]);
  194. } catch (GuzzleException $e) {
  195. Yii::error('push/single/cid: ' . $e->getMessage());
  196. }
  197. // 失败,则生重新获取 token ,再发送
  198. }
  199. public static function getToken()
  200. {
  201. $i = 0;
  202. do{
  203. $client = new Client([
  204. 'base_uri' => self::baseUrl,
  205. 'timeout' => 5.0
  206. ]);
  207. $response = $client->request('POST', self::baseUrl . '/auth', [
  208. 'form_params' => [
  209. "sign" => hash('sha256', self::appKey . time() . self::masterSecret),
  210. "timestamp" => round(microtime(true) * 1000),
  211. "appkey" => self::appKey
  212. ]
  213. ]);
  214. $data = json_decode($response, true);
  215. }while(!isset($data['data']['token']) && $i++ < 3);
  216. if ($i > 3) {
  217. return "";
  218. }
  219. return $data['data']['token'];
  220. }
  221. }