Aucune description

XmPushAndroid.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xinrui
  5. * Date: 2018/5/22
  6. * Time: 下午1:52
  7. */
  8. use xmpush\Builder;
  9. use xmpush\HttpBase;
  10. use xmpush\Sender;
  11. use xmpush\Constants;
  12. use xmpush\Stats;
  13. use xmpush\Tracer;
  14. use xmpush\Feedback;
  15. use xmpush\DevTools;
  16. use xmpush\Subscription;
  17. use xmpush\TargetedMessage;
  18. include_once(dirname(__FILE__) . '/autoload.php');
  19. class XmPushAndroid {
  20. /*
  21. * 立即推送-全部用户
  22. */
  23. public static function androidPushAll($desc)
  24. {
  25. Constants::useOfficial();//正式环境
  26. //Constants::useSandbox();//测试环境
  27. $secret = $desc['secret'];
  28. $package = $desc['package'];
  29. Constants::setSecret($secret);
  30. Constants::setPackage($package);
  31. $message = new Builder();
  32. $message->title($desc['title']); // 通知栏的title
  33. $message->description($desc['message']); // 通知栏的descption
  34. $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
  35. //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
  36. //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
  37. $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
  38. //$message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
  39. $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
  40. //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
  41. if (isset($desc['goods_id'])){
  42. $message->extra('pushType',1000);
  43. $message->extra('goods_id', $desc['goods_id']);
  44. $message->extra('is_coupon',$desc['is_coupon']);
  45. $message->extra('coupon_price',$desc['coupon_price']);
  46. $message->extra('price',$desc['price']);
  47. $message->extra('discount_price',$desc['discount_price']);
  48. $message->extra('commission_rate',$desc['commission_rate']);
  49. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  50. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  51. }elseif(isset($desc['group_id'])){
  52. $message->extra('pushType',1001);
  53. $message->extra('gid', $desc['group_id']);
  54. $message->extra('name',$desc['group_name']);
  55. }else{
  56. if( !empty($desc['url']) ){
  57. $message->extra('pushType',1002);
  58. $message->extra('url',$desc['url']);
  59. }else{
  60. switch ($desc['message_type']) {
  61. case '1':
  62. $pushType = 1003;
  63. break;
  64. case '4':
  65. $pushType = 1004;
  66. break;
  67. case '5':
  68. $pushType = 1005;
  69. break;
  70. case '6':
  71. $pushType = 1006;
  72. break;
  73. default:
  74. $pushType = 0;
  75. break;
  76. }
  77. $message->extra('pushType',$pushType);
  78. }
  79. }
  80. $message->build();
  81. $sender = new Sender();
  82. $re = $sender->broadcastAll($message)->getRaw();
  83. //print_r($re);
  84. return true;
  85. }
  86. /*
  87. * 立即推送-部分用户
  88. */
  89. public static function androidPushPart($desc)
  90. {
  91. Constants::useOfficial();//正式环境
  92. //Constants::useSandbox();//测试环境
  93. $secret = $desc['secret'];
  94. $package = $desc['package'];
  95. Constants::setSecret($secret);
  96. Constants::setPackage($package);
  97. //Constants::setBundleId($package);
  98. $message = new Builder();
  99. $message->title($desc['title']); // 通知栏的title
  100. $message->description($desc['message']); // 通知栏的descption
  101. $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
  102. //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
  103. //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
  104. // $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
  105. $message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
  106. $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
  107. //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
  108. if (isset($desc['goods_id'])){
  109. $message->extra('pushType',1000);
  110. $message->extra('goods_id', $desc['goods_id']);
  111. $message->extra('is_coupon',$desc['is_coupon']);
  112. $message->extra('coupon_price',$desc['coupon_price']);
  113. $message->extra('price',$desc['price']);
  114. $message->extra('discount_price',$desc['discount_price']);
  115. $message->extra('commission_rate',$desc['commission_rate']);
  116. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  117. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  118. }elseif(isset($desc['group_id'])){
  119. $message->extra('pushType',1001);
  120. $message->extra('gid', $desc['group_id']);
  121. $message->extra('name',$desc['group_name']);
  122. }else{
  123. if( !empty($desc['url']) ){
  124. $message->extra('pushType',1002);
  125. $message->extra('url',$desc['url']);
  126. }else{
  127. switch ($desc['message_type']) {
  128. case '1':
  129. $pushType = 1003;
  130. break;
  131. case '4':
  132. $pushType = 1004;
  133. break;
  134. case '5':
  135. $pushType = 1005;
  136. break;
  137. case '6':
  138. $pushType = 1006;
  139. break;
  140. case '10':
  141. $pushType = 1010;
  142. break;
  143. default:
  144. $pushType = 0;
  145. break;
  146. }
  147. $message->extra('pushType',$pushType);
  148. }
  149. }
  150. $message->build();
  151. $sender = new Sender();
  152. if(is_string($desc['users'])){
  153. $re = $sender->sendToAlias($message,$desc['users'])->getRaw();
  154. }else{
  155. $re = $sender->sendToAliases($message,$desc['users'])->getRaw();
  156. }
  157. //print_r($re);
  158. return $re;
  159. }
  160. /*
  161. * 定时推送-全部用户
  162. */
  163. public static function androidPushAllTimingPush($desc)
  164. {
  165. Constants::useOfficial();//正式环境
  166. //Constants::useSandbox();//测试环境
  167. $secret = $desc['secret'];
  168. $package = $desc['package'];
  169. Constants::setSecret($secret);
  170. Constants::setPackage($package);
  171. //Constants::setBundleId($package);
  172. $message = new Builder();
  173. $message->title($desc['title']); // 通知栏的title
  174. $message->description($desc['message']); // 通知栏的descption
  175. $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
  176. //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
  177. //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
  178. $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
  179. //$message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
  180. $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
  181. //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
  182. $message->timeToSend($desc['timetosend']*1000);
  183. if (isset($desc['goods_id'])){
  184. $message->extra('pushType',1000);
  185. $message->extra('goods_id', $desc['goods_id']);
  186. $message->extra('is_coupon',$desc['is_coupon']);
  187. $message->extra('coupon_price',$desc['coupon_price']);
  188. $message->extra('price',$desc['price']);
  189. $message->extra('discount_price',$desc['discount_price']);
  190. $message->extra('commission_rate',$desc['commission_rate']);
  191. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  192. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  193. }elseif(isset($desc['group_id'])){
  194. $message->extra('pushType',1001);
  195. $message->extra('gid', $desc['group_id']);
  196. $message->extra('name',$desc['group_name']);
  197. }else{
  198. if( !empty($desc['url']) ){
  199. $message->extra('pushType',1002);
  200. $message->extra('url',$desc['url']);
  201. }else{
  202. switch ($desc['message_type']) {
  203. case '1':
  204. $pushType = 1003;
  205. break;
  206. case '4':
  207. $pushType = 1004;
  208. break;
  209. case '5':
  210. $pushType = 1005;
  211. break;
  212. case '6':
  213. $pushType = 1006;
  214. break;
  215. default:
  216. $pushType = 0;
  217. break;
  218. }
  219. $message->extra('pushType',$pushType);
  220. }
  221. }
  222. $message->build();
  223. $sender = new Sender();
  224. $re = $sender->broadcastAll($message)->getRaw();
  225. //print_r($re);
  226. return true;
  227. }
  228. /*
  229. * 定时推送-部分用户
  230. */
  231. public static function androidPushPartTimingPush($desc)
  232. {
  233. Constants::useOfficial();//正式环境
  234. //Constants::useSandbox();//测试环境
  235. $secret = $desc['secret'];
  236. $package = $desc['package'];
  237. Constants::setSecret($secret);
  238. Constants::setPackage($package);
  239. //Constants::setBundleId($package);
  240. $message = new Builder();
  241. $message->title($desc['title']); // 通知栏的title
  242. $message->description($desc['message']); // 通知栏的descption
  243. $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
  244. //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
  245. //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
  246. $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
  247. //$message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
  248. $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
  249. //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
  250. $message->timeToSend($desc['timetosend']*1000);
  251. if (isset($desc['goods_id'])){
  252. $message->extra('pushType',1000);
  253. $message->extra('goods_id', $desc['goods_id']);
  254. $message->extra('is_coupon',$desc['is_coupon']);
  255. $message->extra('coupon_price',$desc['coupon_price']);
  256. $message->extra('price',$desc['price']);
  257. $message->extra('discount_price',$desc['discount_price']);
  258. $message->extra('commission_rate',$desc['commission_rate']);
  259. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  260. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  261. }elseif(isset($desc['group_id'])){
  262. $message->extra('pushType',1001);
  263. $message->extra('gid', $desc['group_id']);
  264. $message->extra('name',$desc['group_name']);
  265. }else{
  266. if( !empty($desc['url']) ){
  267. $message->extra('pushType',1002);
  268. $message->extra('url',$desc['url']);
  269. }else{
  270. switch ($desc['message_type']) {
  271. case '1':
  272. $pushType = 1003;
  273. break;
  274. case '4':
  275. $pushType = 1004;
  276. break;
  277. case '5':
  278. $pushType = 1005;
  279. break;
  280. case '6':
  281. $pushType = 1006;
  282. break;
  283. default:
  284. $pushType = 0;
  285. break;
  286. }
  287. $message->extra('pushType',$pushType);
  288. }
  289. }
  290. $message->build();
  291. $sender = new Sender();
  292. if(is_string($desc['users'])){
  293. $re = $sender->sendToAlias($message,$desc['users'])->getRaw();
  294. }else{
  295. $re = $sender->sendToAliases($message,$desc['users'])->getRaw();
  296. }
  297. //print_r($re);
  298. return $re;
  299. }
  300. /*
  301. * 删除定时推送 - 全部用户
  302. */
  303. public static function androidPushAllTimingPushDelete($desc)
  304. {
  305. Constants::useOfficial();//正式环境
  306. //Constants::useSandbox();//测试环境
  307. $secret = $desc['secret'];
  308. $package = $desc['package'];
  309. Constants::setSecret($secret);
  310. Constants::setPackage($package);
  311. $sender = new Sender();
  312. //判断该条推送消息是否有之前版本,有->删除
  313. $messageid=!empty($desc['push_msg_id'])?$desc['push_msg_id']:0;
  314. $flag=$sender->checkScheduleJobExist($messageid); //检测定时消息的任务是否存在
  315. $error_code=$flag->getErrorCode();
  316. //print_r($error_code);
  317. if ($error_code==0){
  318. //有->删除
  319. $flag2=$sender->deleteScheduleJob($messageid);
  320. $error_code2=$flag2->getErrorCode();
  321. //print_r($error_code2);
  322. if ($error_code2==0) {
  323. return true;
  324. }
  325. }else{
  326. return true;
  327. }
  328. }
  329. }
  330. ?>