123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xinrui
- * Date: 2018/4/20
- * Time: 下午5:03
- */
- use xmpush\IOSBuilder;
- use xmpush\Sender;
- use xmpush\Constants;
- use xmpush\Stats;
- use xmpush\Tracer;
- include_once(dirname(__FILE__) . '/autoload.php');
- class XmPushIOS {
- /*
- * 立即推送-全部用户
- */
- public static function iphonePushAll($desc)
- {
- //dd($desc);
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
- $secret = $desc['secret'];
- $bundleId = $desc['bundleId'];
- Constants::setSecret($secret);
- Constants::setBundleId($bundleId);
- $message = new IOSBuilder();
- $message->description($desc['message']);
- $message->body($desc['message']);
- $message->title($desc['title']);
- $message->soundUrl('1');
- $message->badge('1');
- if (isset($desc['goods_id'])){
- $message->extra('pushType',1000);
- $message->extra('goods_id', $desc['goods_id']);
- $message->extra('is_coupon',$desc['is_coupon']);
- $message->extra('coupon_price',$desc['coupon_price']);
- $message->extra('price',$desc['price']);
- $message->extra('discount_price',$desc['discount_price']);
- $message->extra('commission_rate',$desc['commission_rate']);
- $message->extra('coupon_start_time',$desc['coupon_start_time']);
- $message->extra('coupon_end_time',$desc['coupon_end_time']);
- }elseif(isset($desc['group_id'])){
- $message->extra('pushType',1001);
- $message->extra('gid', $desc['group_id']);
- $message->extra('name',$desc['group_name']);
- }else{
- if( !empty($desc['url']) ){
- $message->extra('pushType',1002);
- $message->extra('url',$desc['url']);
- }else{
- switch ($desc['message_type']) {
- case '1':
- $pushType = 1003;
- break;
- case '4':
- $pushType = 1004;
- break;
- case '5':
- $pushType = 1005;
- break;
- case '6':
- $pushType = 1006;
- break;
- default:
- $pushType = 0;
- break;
- }
- $message->extra('pushType',$pushType);
- }
- }
- $message->timeToLive($desc['timetolive']*1000);
- $message->build();
- $sender = new Sender();
- $re = $sender->broadcastAll($message)->getRaw();
- //print_r($re);
- return $re;
- }
- /*
- * 立即推送-部分用户
- */
- public static function iphonePushPart($desc)
- {
-
- Constants::useOfficial();//正式环境
- // Constants::useSandbox();//测试环境
- $secret = $desc['secret'];
- $bundleId = $desc['bundleId'];
- Constants::setSecret($secret);
- Constants::setBundleId($bundleId);
- $message = new IOSBuilder();
- $message->description($desc['message']);
- $message->body($desc['message']);
- $message->title($desc['title']);
- $message->soundUrl('1');
- $message->badge('1');
- if (isset($desc['goods_id'])){
- $message->extra('pushType',1000);
- $message->extra('goods_id', $desc['goods_id']);
- $message->extra('is_coupon',$desc['is_coupon']);
- $message->extra('coupon_price',$desc['coupon_price']);
- $message->extra('price',$desc['price']);
- $message->extra('discount_price',$desc['discount_price']);
- $message->extra('commission_rate',$desc['commission_rate']);
- $message->extra('coupon_start_time',$desc['coupon_start_time']);
- $message->extra('coupon_end_time',$desc['coupon_end_time']);
- }elseif(isset($desc['group_id'])){
- $message->extra('pushType',1001);
- $message->extra('gid', $desc['group_id']);
- $message->extra('name',$desc['group_name']);
- }else{
- if( !empty($desc['url']) ){
- $message->extra('pushType',1002);
- $message->extra('url',$desc['url']);
- }else{
- switch ($desc['message_type']) {
- case '1':
- $pushType = 1003;
- break;
- case '4':
- $pushType = 1004;
- break;
- case '5':
- $pushType = 1005;
- break;
- case '6':
- $pushType = 1006;
- break;
- case '10':
- $pushType = 1010;
- break;
- default:
- $pushType = 0;
- break;
- }
- $message->extra('pushType',$pushType);
- }
- }
- $message->timeToLive($desc['timetolive']*1000);
- $message->build();
- $sender = new Sender();
-
- if(is_string($desc['users'])){
- $re = $sender->sendToAlias($message,$desc['users'])->getRaw();
- }else{
- $re = $sender->sendToAliases($message,$desc['users'])->getRaw();
- }
- //print_r($re);
- return $re;
- }
- /*
- * 定时推送-全部用户
- */
- public static function iphonePushAllTimingPush($desc)
- {
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
-
- $secret = $desc['secret'];
- $bundleId = $desc['bundleId'];
- Constants::setSecret($secret);
- Constants::setBundleId($bundleId);
- $message = new IOSBuilder();
- $message->description($desc['message']);
- $message->body($desc['message']);
- $message->title($desc['title']);
- $message->soundUrl('1');
- $message->badge('1');
- if (isset($desc['goods_id'])){
- $message->extra('pushType',1000);
- $message->extra('goods_id', $desc['goods_id']);
- $message->extra('is_coupon',$desc['is_coupon']);
- $message->extra('coupon_price',$desc['coupon_price']);
- $message->extra('price',$desc['price']);
- $message->extra('discount_price',$desc['discount_price']);
- $message->extra('commission_rate',$desc['commission_rate']);
- $message->extra('coupon_start_time',$desc['coupon_start_time']);
- $message->extra('coupon_end_time',$desc['coupon_end_time']);
- }elseif(isset($desc['group_id'])){
- $message->extra('pushType',1001);
- $message->extra('gid', $desc['group_id']);
- $message->extra('name',$desc['group_name']);
- }else{
- if( !empty($desc['url']) ){
- $message->extra('pushType',1002);
- $message->extra('url',$desc['url']);
- }else{
- switch ($desc['message_type']) {
- case '1':
- $pushType = 1003;
- break;
- case '4':
- $pushType = 1004;
- break;
- case '5':
- $pushType = 1005;
- break;
- case '6':
- $pushType = 1006;
- break;
- default:
- $pushType = 0;
- break;
- }
- $message->extra('pushType',$pushType);
- }
- }
- $message->timeToLive($desc['timetolive']*1000);
- $message->timeToSend($desc['timetosend']*1000);
- $message->build();
- $sender = new Sender();
- $re = $sender->broadcastAll($message)->getRaw();
- //判断该条推送消息是否有之前版本,有->删除
- if ($re['code']==0){
- self::iphonePushAllTimingPushDelete($desc);
- // //print_r($messageid);
- // $flag=$sender->checkScheduleJobExist($messageid); //检测定时消息的任务是否存在
- // $error_code=$flag->getErrorCode();
- // if ($error_code==0){
- // //有->删除
- // $sender->deleteScheduleJob($messageid);
- // }
- }
- //echo '<pre>';
- //print_r($re);
- return $re;
- }
- /*
- * 定时推送-部分用户
- */
- public static function iphonePushPartTimingPush($desc)
- {
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
-
- $secret = $desc['secret'];
- $bundleId = $desc['bundleId'];
- Constants::setSecret($secret);
- Constants::setBundleId($bundleId);
- $message = new IOSBuilder();
- $message->description($desc['message']);
- $message->body($desc['message']);
- $message->title($desc['title']);
- $message->soundUrl('1');
- $message->badge('1');
- if (isset($desc['goods_id'])){
- $message->extra('pushType',1000);
- $message->extra('goods_id', $desc['goods_id']);
- $message->extra('is_coupon',$desc['is_coupon']);
- $message->extra('coupon_price',$desc['coupon_price']);
- $message->extra('price',$desc['price']);
- $message->extra('discount_price',$desc['discount_price']);
- $message->extra('commission_rate',$desc['commission_rate']);
- $message->extra('coupon_start_time',$desc['coupon_start_time']);
- $message->extra('coupon_end_time',$desc['coupon_end_time']);
- }elseif(isset($desc['group_id'])){
- $message->extra('pushType',1001);
- $message->extra('gid', $desc['group_id']);
- $message->extra('name',$desc['group_name']);
- }else{
- if( !empty($desc['url']) ){
- $message->extra('pushType',1002);
- $message->extra('url',$desc['url']);
- }else{
- switch ($desc['message_type']) {
- case '1':
- $pushType = 1003;
- break;
- case '4':
- $pushType = 1004;
- break;
- case '5':
- $pushType = 1005;
- break;
- case '6':
- $pushType = 1006;
- break;
- default:
- $pushType = 0;
- break;
- }
- $message->extra('pushType',$pushType);
- }
- }
- $message->timeToLive($desc['timetolive']*1000);
- $message->timeToSend($desc['timetosend']*1000);
- $message->build();
- $sender = new Sender();
- if(is_string($desc['users'])){
- $re = $sender->sendToAlias($message,$desc['users'])->getRaw();
- }else{
- $re = $sender->sendToAliases($message,$desc['users'])->getRaw();
- }
- // //判断该条推送消息是否有之前版本,有->删除
- // if ($re['code']==0){
- // $messageid=!empty($desc['push_msg_id'])?$desc['push_msg_id']:0;
- // //print_r($messageid);
- // $flag=$sender->checkScheduleJobExist($messageid); //检测定时消息的任务是否存在
- // $error_code=$flag->getErrorCode();
- // if ($error_code==0){
- // //有->删除
- // deleteScheduleJob($messageid);
- // }
- // }
- // return $re;
- //print_r($re);
- return $re;
- }
- /*
- * 删除定时推送 - 全部用户
- */
- public static function iphonePushAllTimingPushDelete($desc)
- {
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
-
- $secret = $desc['secret'];
- $bundleId = $desc['bundleId'];
- Constants::setSecret($secret);
- Constants::setBundleId($bundleId);
- $sender = new Sender();
- //判断该条推送消息是否有之前版本,有->删除
- $messageid=!empty($desc['push_msg_id'])?$desc['push_msg_id']:0;
- $flag=$sender->checkScheduleJobExist($messageid); //检测定时消息的任务是否存在
- $error_code=$flag->getErrorCode();
- //print_r($error_code);
- if ($error_code==0){
- //有->删除
- $flag2=$sender->deleteScheduleJob($messageid);
- $error_code2=$flag2->getErrorCode();
- //print_r($error_code2);
- if ($error_code2==0) {
- return true;
- }
- }else{
- return true;
- }
- }
-
- }
|