pushNotice.php 8.9 KB

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