pushNotice.php 8.6 KB

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