GeTuiService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace services\extend\push;
  3. use GeTui\Client;
  4. use Yii;
  5. use yii\helpers\Json;
  6. use common\components\Service;
  7. use common\helpers\ArrayHelper;
  8. /**
  9. * 个推
  10. *
  11. * Yii::$app->services->extendGeTui->client()->ios($title, $content, $clientId, $transmissionContent = [])
  12. *
  13. * Class GeTuiService
  14. * @package services\extend
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class GeTuiService extends Service
  18. {
  19. /**
  20. * @var Client
  21. */
  22. protected $client;
  23. /**
  24. * @param array $config
  25. * @return $this
  26. */
  27. public function client($config = [])
  28. {
  29. $config = ArrayHelper::merge([
  30. 'app_key' => Yii::$app->services->config->backendConfig('getui_app_id'),
  31. 'app_id' => Yii::$app->services->config->backendConfig('getui_app_key'),
  32. 'master_secret' => Yii::$app->services->config->backendConfig('getui_master_secret'),
  33. 'logo_url' => Yii::$app->services->config->backendConfig('getui_logo_url')
  34. ], $config);
  35. try {
  36. $this->client = new Client($config);
  37. } catch (\GeTui\ApiException $apiException) {
  38. }
  39. return $this;
  40. }
  41. /**
  42. * ios 推送
  43. *
  44. * @param $title
  45. * @param $content
  46. * @param $clientId
  47. * @param array $transmissionContent 透传内容
  48. */
  49. public function ios($title, $content, $clientId, $transmissionContent = [])
  50. {
  51. try {
  52. $client = $this->client;
  53. /**
  54. * IOS推送详细设置
  55. */
  56. $ret = $client->single->setClientId($clientId)
  57. ->setNotification(function (\GeTui\Template\Notification $notification) use ($transmissionContent) {
  58. /** (安卓有效)收到消息是否立即启动应用,true为立即启动,false则广播等待启动,默认是否 */
  59. $notification->setTransmissionType(false);
  60. /** 设置透传的内容 */
  61. $notification->setTransmissionContent(Json::encode($transmissionContent));
  62. /** 设定展示开始时间,格式为yyyy-MM-dd HH:mm:ss */
  63. $notification->setDurationBegin(date('Y-m-d H:i:s'));
  64. /** 设定展示结束时间,格式为yyyy-MM-dd HH:mm:ss */
  65. $notification->setDurationBegin(date('Y-m-d H:i:s', time() + 3600 * 24));
  66. })
  67. ->setPushInfo(function (\GeTui\Template\PushInfo $pushInfo) use ($title, $content) {
  68. //(必传)通知标题
  69. $pushInfo->setTitle($title);
  70. //(必传)通知内容
  71. $pushInfo->setBody($content);
  72. //自定义透传内容
  73. // $pushInfo->setPayload(json_encode(['name' => "周先生"]));
  74. //推送直接带有透传数据 默认1,可选-1
  75. // $pushInfo->setContentAvailable(1);
  76. //用于计算icon上显示的数字,还可以实现显示数字的自动增减,如“+1”、 “-1”、 “1” 等,计算结果将覆盖badge,默认是+1
  77. $pushInfo->setAutoBadge('+1');
  78. //通知铃声文件名,无声设置为“com.gexin.ios.silence”,默认default
  79. $pushInfo->setSound('default');
  80. //多媒体设置
  81. //资源类型(1.图片,2.音频, 3.视频)
  82. // $pushInfo->setType(1);
  83. //是否只在wifi环境下加载,如果设置成true,但未使用wifi时,会展示成普通通知,默认是false
  84. // $pushInfo->setOnlyWifi(false);
  85. //多媒体资源地址
  86. // $pushInfo->setUrl('');
  87. })
  88. ->push();
  89. } catch (\GeTui\ApiException $apiException) {
  90. Yii::error($apiException->getMessage());
  91. }
  92. }
  93. /**
  94. * 安卓推送
  95. *
  96. * @param $title
  97. * @param $content
  98. * @param $clientId
  99. * @param array $transmissionContent 透传内容
  100. */
  101. public function android($title, $content, $clientId, $transmissionContent = [])
  102. {
  103. try {
  104. $client = $this->client;
  105. /**
  106. * 别名推送安卓应用推送
  107. * 这里别名和设备号不可以重复设置,只有其一生效
  108. */
  109. $client->single->setClientId($clientId)
  110. ->setStyle(function (\GeTui\Template\Style $style) use ($title, $content) {
  111. /** 设置标题(必传) */
  112. $style->setTitle($title);
  113. /** 设置内容(必传) */
  114. $style->setText($content);
  115. /** 如果配置中设置过了,那这里就不需要设置,如果设置了将覆盖配置的url */
  116. // $style->setLogourl('');
  117. /** 设置logo,这个是APP本地文件的logo名称,一般不会设置,直接使用logo_url */
  118. // $style->setLogo('');
  119. /** 通知是否可清除: true可清除,false不可清除。默认可清除 */
  120. $style->setIsClearable(true);
  121. /** 收到通知是否振动:true振动,false不振动。默认振动 */
  122. $style->setIsVibrate(true);
  123. /** 收到通知是否响铃:true响铃,false不响铃。默认响铃 */
  124. $style->setIsRing(true);
  125. /** 背景图样式,设置背景图的url地址 */
  126. $style->setBannerUrl('');
  127. /** 展开通知样式,设置类型
  128. * 1 通知展示大图样式,参数是大图的URL地址
  129. * 2 通知展示文本+长文本样式,参数是长文本
  130. * 3 通知展示大图+小图样式,参数是大图URL和小图URL
  131. */
  132. $style->setBigStyle(1);
  133. //展开通知样式 1
  134. $style->setBigImageUrl('');
  135. //展开通知样式 2
  136. $style->setBigText('');
  137. //展开通知样式 3,这个是大图和背景图的2个参数组合
  138. //$style->setBigImageUrl('');
  139. //$style->setBannerUrl('');
  140. });
  141. /** 打开应用模板消息(模板消息只可以设置一种) */
  142. $client->single->setNotification(function (\GeTui\Template\Notification $notification) use ($client, $transmissionContent) {
  143. //收到消息是否立即启动应用,true为立即启动,false则广播等待启动,默认是否
  144. $notification->setTransmissionType(false);
  145. //(可空)透传字符串
  146. $notification->setTransmissionContent(Json::encode($transmissionContent));
  147. //(必传)设置样式,结合Style的设置获取指定的样式类型:system(个推),getui(个推),banner(背景图),expand(展开)
  148. $notification->setStyle($client->single->getStyle());
  149. //设定展示开始时间,格式为yyyy-MM-dd HH:mm:ss
  150. // $notification->setDurationBegin(date('Y-m-d H:i:s'));
  151. //设定展示结束时间,格式为yyyy-MM-dd HH:mm:ss
  152. // $notification->setDurationEnd(date('Y-m-d H:i:s', time() + 3600 * 24));
  153. });
  154. /** 打开网页模板消息(模板消息只可以设置一种) */
  155. // $client->single->setLink(function (\GeTui\Template\Link $link) use ($client) {
  156. // //(必传)打开网址的地址
  157. // $link->setUrl('');
  158. // //(必传)设置样式,结合Style的设置获取指定的样式类型:system(个推),getui(个推),banner(背景图),expand(展开)
  159. // $link->setStyle($client->single->getStyle());
  160. // //设定展示开始时间,格式为yyyy-MM-dd HH:mm:ss
  161. // $link->setDurationBegin('2018-05-06 12:00:00');
  162. // //设定展示结束时间,格式为yyyy-MM-dd HH:mm:ss
  163. // $link->setDurationEnd('2018-05-08 12:00:00');
  164. // });
  165. /** 弹窗下模板消息(模板消息只可以设置一种) */
  166. // $client->single->setNotypopload(function (\GeTui\Template\Notypopload $notypopload) use ($client) {
  167. // //(必传)设置样式,结合Style的设置获取指定的样式类型:system(个推),getui(个推),banner(背景图),expand(展开)
  168. // $notypopload->setStyle($client->single->getStyle());
  169. // //(必传)通知栏图标
  170. // $notypopload->setNotyicon();
  171. // //(必传)通知标题
  172. // $notypopload->setNotytitle();
  173. // //(必传)通知内容
  174. // $notypopload->setNotycontent();
  175. // //(必传)弹出框标题
  176. // $notypopload->setPoptitle();
  177. // //(必传)弹出框内容
  178. // $notypopload->setPopcontent();
  179. // //(必传)弹出框图标
  180. // $notypopload->setPopimage();
  181. // //(必传)弹出框左边按钮名称
  182. // $notypopload->setPopbutton1();
  183. // //(必传)弹出框右边按钮名称
  184. // $notypopload->setPopbutton2();
  185. // //现在图标
  186. // $notypopload->setLoadicon();
  187. // //下载标题
  188. // $notypopload->setLoadtitle();
  189. // //(必传)下载文件地址
  190. // $notypopload->setLoadurl();
  191. // //是否自动安装,默认值false
  192. // $notypopload->setIsAutoinstall();
  193. // //安装完成后是否自动启动应用程序,默认值false
  194. // $notypopload->setIsActived();
  195. // //设定展示开始时间,格式为yyyy-MM-dd HH:mm:ss
  196. // $notypopload->setDurationBegin('2018-05-06 12:00:00');
  197. // //设定展示结束时间,格式为yyyy-MM-dd HH:mm:ss
  198. // $notypopload->setDurationEnd('2018-05-08 12:00:00');
  199. // });
  200. /** 推送消息 */
  201. $ret = $client->single->push();
  202. } catch (\GeTui\ApiException $apiException) {
  203. Yii::error($apiException->getMessage());
  204. }
  205. }
  206. /**
  207. * 任务投递推送
  208. *
  209. * @param $title
  210. * @param $content
  211. * @param array $clientIds
  212. */
  213. public function task($title, $content, array $clientIds)
  214. {
  215. try {
  216. $client = $this->client;
  217. /**
  218. * 任务投递推送
  219. * 这里的推送模板设置,
  220. * 设置别名和设备号只可以选一种类型去推送,不可二者都选
  221. * 这里的save是把任务保存在个推服务器
  222. */
  223. $client->task->setClientId($clientIds)
  224. ->setPushInfo(function (\GeTui\Template\PushInfo $pushInfo) use ($title, $content) {
  225. $pushInfo->setTitle($title);
  226. $pushInfo->setBody($content);
  227. })
  228. ->save();
  229. //获取推送的任务ID,如果需要保存taskId的话,可以用这种方式获取,不过基本上没啥用处
  230. $taskId = $client->task->taskId;
  231. //执行推送操作,推送到指定的设备列表,传入参数是否展示推送任务的详情,默认false不展示
  232. $client->task->run(false);
  233. } catch (\GeTui\ApiException $apiException) {
  234. }
  235. }
  236. /**
  237. * 根据个推条件批量推送
  238. *
  239. * @param $title
  240. * @param $content
  241. * @param array $conditions
  242. * [
  243. * ["key" => "phonetype", "values" => ["ANDROID"], "opt_type" => 0],//条件设备类型为安卓设备
  244. * ["key" => "region", "values" => ["11000000", "12000000"], "opt_type" => 0],//指定区域
  245. * ["key" => "tag", "values" => ["usertag"], "opt_type" => 0]//指定用户标签
  246. * ]
  247. */
  248. public function batch($title, $content, array $conditions)
  249. {
  250. try {
  251. $client = $this->client;
  252. $client->batch->setPushInfo(function (\GeTui\Template\PushInfo $pushInfo) use ($title, $content) {
  253. $pushInfo->setTitle($title);
  254. $pushInfo->setBody($content);
  255. });
  256. /**
  257. * key 筛选条件类型名称(省市region,手机类型phonetype,用户标签tag)
  258. * value 筛选参数
  259. * opt_type 筛选参数的组合,0:取参数并集or,1:交集and,2:相当与not in {参数1,参数2,....}
  260. */
  261. $client->batch->setConditions($conditions);
  262. //开始推送给指定的筛选条件,只个条件是个推的服务器条件
  263. $client->batch->push();
  264. } catch (\GeTui\ApiException $apiException) {
  265. }
  266. }
  267. /**
  268. * api接口调用
  269. */
  270. public function api()
  271. {
  272. try {
  273. $client = $this->client;
  274. /**
  275. * api实现了个推推送接口的全部操作
  276. * 传入参数请参考文档地址的参数
  277. * 基本上传入的参数都是基本一致的,有的数组参数会做一个基础的封装
  278. * 例如绑定别名的操作接口
  279. */
  280. $ret = $client->api->bindAlias([
  281. ['cid' => '1', 'alias' => '11'],
  282. ['cid' => '2', 'alias' => '22'],
  283. ]);
  284. } catch (\GeTui\ApiException $apiException) {
  285. }
  286. }
  287. }
粤ICP备19079148号