pushNotice.php 8.5 KB

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