pushNotice.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. public static function push($adminId, $cId, $title, $body)
  27. {
  28. $notify = new GTNotification();
  29. $notify->setTitle($title);
  30. $notify->setBody($body);
  31. //点击通知后续动作,目前支持以下后续动作:
  32. //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
  33. $notify->setClickType("startapp");
  34. //保证有振动
  35. $notify->setChannelId('channelId1');
  36. $notify->setChannelName('channelName1');
  37. $notify->setChannelLevel(4);
  38. $message = new GTPushMessage();
  39. $message->setNotification($notify);
  40. //设置推送参数
  41. $push = new GTPushRequest();
  42. $time = time();
  43. $rand = rand(1000, 9999);
  44. $rId = $time . $rand . $adminId;
  45. $push->setRequestId($rId);
  46. $push->setPushMessage($message);
  47. $push->setCid($cId);
  48. //创建API,APPID等配置参考 环境要求 进行获取
  49. $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
  50. $api->pushApi()->pushToSingleByCid($push);
  51. }
  52. public static function gtPush($cId, $title, $body)
  53. {
  54. Yii::info('start -- push/single/cid');
  55. $set = new GTSettings();
  56. $set->setTtl(3600000);
  57. $strategy = new GTStrategy();
  58. $strategy->setDefault(GTStrategy::STRATEGY_THIRD_FIRST);
  59. // $strategy->setIos(GTStrategy::STRATEGY_GT_ONLY);
  60. // $strategy->setOp(GTStrategy::STRATEGY_THIRD_FIRST);
  61. // $strategy->setHw(GTStrategy::STRATEGY_THIRD_ONLY);
  62. $set->setStrategy($strategy);
  63. //设置推送参数
  64. $push = new GTPushRequest();
  65. $push->setRequestId(self::micro_time());
  66. $push->setSettings($set);
  67. $message = new GTPushMessage();
  68. $notify = new GTNotification();
  69. $notify->setTitle($title);
  70. $notify->setBody($body);
  71. $notify->setChannelId('channelId1');
  72. $notify->setChannelName('channelName1');
  73. $notify->setChannelLevel(4);
  74. //点击通知后续动作,目前支持以下后续动作:
  75. //1、intent:打开应用内特定页面 url:打开网页地址。2、payload:自定义消息内容启动应用。
  76. //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($title);
  97. $alert->setBody($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. // ------------- ios end ---------------
  121. // ----------------- 安卓 -----------------
  122. $android = new GTAndroid();
  123. $ups = new GTUps();
  124. //$ups->setTransmission("ups Transmission");
  125. $ups->addOption("HW","badgeAddNum",1);
  126. $ups->addOption("HW", "/message/android/urgency","HIGH");
  127. $ups->addOption("HW", "/message/android/category", "WORK");
  128. $ups->addOption("HW", "/message/android/notification/importance", "NORMAL");
  129. //$ups->addOption("OP","channel","Default");
  130. $ups->addOption("OP","/classification",1);
  131. //$ups->addOption(null,"a","b");
  132. $thirdNotification = new GTThirdNotification();
  133. $thirdNotification->setTitle($title);
  134. $thirdNotification->setBody($body);
  135. $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_STAERAPP);
  136. //$thirdNotification->setIntent("intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end");
  137. //$thirdNotification->setUrl("http://docs.getui.com/getui/server/rest_v2/push/");
  138. //$thirdNotification->setPayload("payload");
  139. $thirdNotification->setNotifyId(time());
  140. $ups->setNotification($thirdNotification);
  141. $android->setUps($ups);
  142. $pushChannel->setAndroid($android);
  143. // ----------------- 安卓 end -----------------
  144. $push->setPushChannel($pushChannel);
  145. //创建API,APPID等配置
  146. $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
  147. //处理返回结果
  148. $result = $api->pushApi()->pushToSingleByCid($push);
  149. Yii::info('push/single/cid: ' . json_encode($result));
  150. }
  151. public static function micro_time()
  152. {
  153. list($usec, $sec) = explode(" ", microtime());
  154. $time = ($sec . substr($usec, 2, 3));
  155. return $time;
  156. }
  157. }