説明なし

XmPushIOS.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xinrui
  5. * Date: 2018/4/20
  6. * Time: 下午5:03
  7. */
  8. use xmpush\IOSBuilder;
  9. use xmpush\Sender;
  10. use xmpush\Constants;
  11. use xmpush\Stats;
  12. use xmpush\Tracer;
  13. include_once(dirname(__FILE__) . '/autoload.php');
  14. class XmPushIOS {
  15. /*
  16. * 立即推送-全部用户
  17. */
  18. public static function iphonePushAll($desc)
  19. {
  20. //dd($desc);
  21. Constants::useOfficial();//正式环境
  22. //Constants::useSandbox();//测试环境
  23. $secret = $desc['secret'];
  24. $bundleId = $desc['bundleId'];
  25. Constants::setSecret($secret);
  26. Constants::setBundleId($bundleId);
  27. $message = new IOSBuilder();
  28. $message->description($desc['message']);
  29. $message->body($desc['message']);
  30. $message->title($desc['title']);
  31. $message->soundUrl('1');
  32. $message->badge('1');
  33. if (isset($desc['goods_id'])){
  34. $message->extra('pushType',1000);
  35. $message->extra('goods_id', $desc['goods_id']);
  36. $message->extra('is_coupon',$desc['is_coupon']);
  37. $message->extra('coupon_price',$desc['coupon_price']);
  38. $message->extra('price',$desc['price']);
  39. $message->extra('discount_price',$desc['discount_price']);
  40. $message->extra('commission_rate',$desc['commission_rate']);
  41. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  42. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  43. }elseif(isset($desc['group_id'])){
  44. $message->extra('pushType',1001);
  45. $message->extra('gid', $desc['group_id']);
  46. $message->extra('name',$desc['group_name']);
  47. }else{
  48. if( !empty($desc['url']) ){
  49. $message->extra('pushType',1002);
  50. $message->extra('url',$desc['url']);
  51. }else{
  52. switch ($desc['message_type']) {
  53. case '1':
  54. $pushType = 1003;
  55. break;
  56. case '4':
  57. $pushType = 1004;
  58. break;
  59. case '5':
  60. $pushType = 1005;
  61. break;
  62. case '6':
  63. $pushType = 1006;
  64. break;
  65. default:
  66. $pushType = 0;
  67. break;
  68. }
  69. $message->extra('pushType',$pushType);
  70. }
  71. }
  72. $message->timeToLive($desc['timetolive']*1000);
  73. $message->build();
  74. $sender = new Sender();
  75. $re = $sender->broadcastAll($message)->getRaw();
  76. //print_r($re);
  77. return $re;
  78. }
  79. /*
  80. * 立即推送-部分用户
  81. */
  82. public static function iphonePushPart($desc)
  83. {
  84. Constants::useOfficial();//正式环境
  85. // Constants::useSandbox();//测试环境
  86. $secret = $desc['secret'];
  87. $bundleId = $desc['bundleId'];
  88. Constants::setSecret($secret);
  89. Constants::setBundleId($bundleId);
  90. $message = new IOSBuilder();
  91. $message->description($desc['message']);
  92. $message->body($desc['message']);
  93. $message->title($desc['title']);
  94. $message->soundUrl('1');
  95. $message->badge('1');
  96. if (isset($desc['goods_id'])){
  97. $message->extra('pushType',1000);
  98. $message->extra('goods_id', $desc['goods_id']);
  99. $message->extra('is_coupon',$desc['is_coupon']);
  100. $message->extra('coupon_price',$desc['coupon_price']);
  101. $message->extra('price',$desc['price']);
  102. $message->extra('discount_price',$desc['discount_price']);
  103. $message->extra('commission_rate',$desc['commission_rate']);
  104. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  105. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  106. }elseif(isset($desc['group_id'])){
  107. $message->extra('pushType',1001);
  108. $message->extra('gid', $desc['group_id']);
  109. $message->extra('name',$desc['group_name']);
  110. }else{
  111. if( !empty($desc['url']) ){
  112. $message->extra('pushType',1002);
  113. $message->extra('url',$desc['url']);
  114. }else{
  115. switch ($desc['message_type']) {
  116. case '1':
  117. $pushType = 1003;
  118. break;
  119. case '4':
  120. $pushType = 1004;
  121. break;
  122. case '5':
  123. $pushType = 1005;
  124. break;
  125. case '6':
  126. $pushType = 1006;
  127. break;
  128. case '10':
  129. $pushType = 1010;
  130. break;
  131. default:
  132. $pushType = 0;
  133. break;
  134. }
  135. $message->extra('pushType',$pushType);
  136. }
  137. }
  138. $message->timeToLive($desc['timetolive']*1000);
  139. $message->build();
  140. $sender = new Sender();
  141. if(is_string($desc['users'])){
  142. $re = $sender->sendToAlias($message,$desc['users'])->getRaw();
  143. }else{
  144. $re = $sender->sendToAliases($message,$desc['users'])->getRaw();
  145. }
  146. //print_r($re);
  147. return $re;
  148. }
  149. /*
  150. * 定时推送-全部用户
  151. */
  152. public static function iphonePushAllTimingPush($desc)
  153. {
  154. Constants::useOfficial();//正式环境
  155. //Constants::useSandbox();//测试环境
  156. $secret = $desc['secret'];
  157. $bundleId = $desc['bundleId'];
  158. Constants::setSecret($secret);
  159. Constants::setBundleId($bundleId);
  160. $message = new IOSBuilder();
  161. $message->description($desc['message']);
  162. $message->body($desc['message']);
  163. $message->title($desc['title']);
  164. $message->soundUrl('1');
  165. $message->badge('1');
  166. if (isset($desc['goods_id'])){
  167. $message->extra('pushType',1000);
  168. $message->extra('goods_id', $desc['goods_id']);
  169. $message->extra('is_coupon',$desc['is_coupon']);
  170. $message->extra('coupon_price',$desc['coupon_price']);
  171. $message->extra('price',$desc['price']);
  172. $message->extra('discount_price',$desc['discount_price']);
  173. $message->extra('commission_rate',$desc['commission_rate']);
  174. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  175. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  176. }elseif(isset($desc['group_id'])){
  177. $message->extra('pushType',1001);
  178. $message->extra('gid', $desc['group_id']);
  179. $message->extra('name',$desc['group_name']);
  180. }else{
  181. if( !empty($desc['url']) ){
  182. $message->extra('pushType',1002);
  183. $message->extra('url',$desc['url']);
  184. }else{
  185. switch ($desc['message_type']) {
  186. case '1':
  187. $pushType = 1003;
  188. break;
  189. case '4':
  190. $pushType = 1004;
  191. break;
  192. case '5':
  193. $pushType = 1005;
  194. break;
  195. case '6':
  196. $pushType = 1006;
  197. break;
  198. default:
  199. $pushType = 0;
  200. break;
  201. }
  202. $message->extra('pushType',$pushType);
  203. }
  204. }
  205. $message->timeToLive($desc['timetolive']*1000);
  206. $message->timeToSend($desc['timetosend']*1000);
  207. $message->build();
  208. $sender = new Sender();
  209. $re = $sender->broadcastAll($message)->getRaw();
  210. //判断该条推送消息是否有之前版本,有->删除
  211. if ($re['code']==0){
  212. self::iphonePushAllTimingPushDelete($desc);
  213. // //print_r($messageid);
  214. // $flag=$sender->checkScheduleJobExist($messageid); //检测定时消息的任务是否存在
  215. // $error_code=$flag->getErrorCode();
  216. // if ($error_code==0){
  217. // //有->删除
  218. // $sender->deleteScheduleJob($messageid);
  219. // }
  220. }
  221. //echo '<pre>';
  222. //print_r($re);
  223. return $re;
  224. }
  225. /*
  226. * 定时推送-部分用户
  227. */
  228. public static function iphonePushPartTimingPush($desc)
  229. {
  230. Constants::useOfficial();//正式环境
  231. //Constants::useSandbox();//测试环境
  232. $secret = $desc['secret'];
  233. $bundleId = $desc['bundleId'];
  234. Constants::setSecret($secret);
  235. Constants::setBundleId($bundleId);
  236. $message = new IOSBuilder();
  237. $message->description($desc['message']);
  238. $message->body($desc['message']);
  239. $message->title($desc['title']);
  240. $message->soundUrl('1');
  241. $message->badge('1');
  242. if (isset($desc['goods_id'])){
  243. $message->extra('pushType',1000);
  244. $message->extra('goods_id', $desc['goods_id']);
  245. $message->extra('is_coupon',$desc['is_coupon']);
  246. $message->extra('coupon_price',$desc['coupon_price']);
  247. $message->extra('price',$desc['price']);
  248. $message->extra('discount_price',$desc['discount_price']);
  249. $message->extra('commission_rate',$desc['commission_rate']);
  250. $message->extra('coupon_start_time',$desc['coupon_start_time']);
  251. $message->extra('coupon_end_time',$desc['coupon_end_time']);
  252. }elseif(isset($desc['group_id'])){
  253. $message->extra('pushType',1001);
  254. $message->extra('gid', $desc['group_id']);
  255. $message->extra('name',$desc['group_name']);
  256. }else{
  257. if( !empty($desc['url']) ){
  258. $message->extra('pushType',1002);
  259. $message->extra('url',$desc['url']);
  260. }else{
  261. switch ($desc['message_type']) {
  262. case '1':
  263. $pushType = 1003;
  264. break;
  265. case '4':
  266. $pushType = 1004;
  267. break;
  268. case '5':
  269. $pushType = 1005;
  270. break;
  271. case '6':
  272. $pushType = 1006;
  273. break;
  274. default:
  275. $pushType = 0;
  276. break;
  277. }
  278. $message->extra('pushType',$pushType);
  279. }
  280. }
  281. $message->timeToLive($desc['timetolive']*1000);
  282. $message->timeToSend($desc['timetosend']*1000);
  283. $message->build();
  284. $sender = new Sender();
  285. if(is_string($desc['users'])){
  286. $re = $sender->sendToAlias($message,$desc['users'])->getRaw();
  287. }else{
  288. $re = $sender->sendToAliases($message,$desc['users'])->getRaw();
  289. }
  290. // //判断该条推送消息是否有之前版本,有->删除
  291. // if ($re['code']==0){
  292. // $messageid=!empty($desc['push_msg_id'])?$desc['push_msg_id']:0;
  293. // //print_r($messageid);
  294. // $flag=$sender->checkScheduleJobExist($messageid); //检测定时消息的任务是否存在
  295. // $error_code=$flag->getErrorCode();
  296. // if ($error_code==0){
  297. // //有->删除
  298. // deleteScheduleJob($messageid);
  299. // }
  300. // }
  301. // return $re;
  302. //print_r($re);
  303. return $re;
  304. }
  305. /*
  306. * 删除定时推送 - 全部用户
  307. */
  308. public static function iphonePushAllTimingPushDelete($desc)
  309. {
  310. Constants::useOfficial();//正式环境
  311. //Constants::useSandbox();//测试环境
  312. $secret = $desc['secret'];
  313. $bundleId = $desc['bundleId'];
  314. Constants::setSecret($secret);
  315. Constants::setBundleId($bundleId);
  316. $sender = new Sender();
  317. //判断该条推送消息是否有之前版本,有->删除
  318. $messageid=!empty($desc['push_msg_id'])?$desc['push_msg_id']:0;
  319. $flag=$sender->checkScheduleJobExist($messageid); //检测定时消息的任务是否存在
  320. $error_code=$flag->getErrorCode();
  321. //print_r($error_code);
  322. if ($error_code==0){
  323. //有->删除
  324. $flag2=$sender->deleteScheduleJob($messageid);
  325. $error_code2=$flag2->getErrorCode();
  326. //print_r($error_code2);
  327. if ($error_code2==0) {
  328. return true;
  329. }
  330. }else{
  331. return true;
  332. }
  333. }
  334. }