123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xinrui
- * Date: 2018/5/22
- * Time: 下午1:52
- */
- use xmpush\Builder;
- use xmpush\HttpBase;
- use xmpush\Sender;
- use xmpush\Constants;
- use xmpush\Stats;
- use xmpush\Tracer;
- use xmpush\Feedback;
- use xmpush\DevTools;
- use xmpush\Subscription;
- use xmpush\TargetedMessage;
- include_once(dirname(__FILE__) . '/autoload.php');
- class XmPushAndroid {
- /*
- * 立即推送-全部用户
- */
- public static function androidPushAll($desc)
- {
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
-
- $secret = $desc['secret'];
- $package = $desc['package'];
-
- Constants::setSecret($secret);
- Constants::setPackage($package);
- $message = new Builder();
- $message->title($desc['title']); // 通知栏的title
- $message->description($desc['message']); // 通知栏的descption
- $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
- //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
- //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
- $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
- //$message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
- $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
- //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
- 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->build();
- $sender = new Sender();
- $re = $sender->broadcastAll($message)->getRaw();
- //print_r($re);
- return true;
- }
- /*
- * 立即推送-部分用户
- */
- public static function androidPushPart($desc)
- {
-
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
- $secret = $desc['secret'];
- $package = $desc['package'];
- Constants::setSecret($secret);
- Constants::setPackage($package);
- //Constants::setBundleId($package);
- $message = new Builder();
- $message->title($desc['title']); // 通知栏的title
- $message->description($desc['message']); // 通知栏的descption
- $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
- //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
- //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
- // $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
- $message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
- $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
- //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
- 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->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 androidPushAllTimingPush($desc)
- {
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
- $secret = $desc['secret'];
- $package = $desc['package'];
- Constants::setSecret($secret);
- Constants::setPackage($package);
- //Constants::setBundleId($package);
- $message = new Builder();
- $message->title($desc['title']); // 通知栏的title
- $message->description($desc['message']); // 通知栏的descption
- $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
- //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
- //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
- $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
- //$message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
- $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
- //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
- $message->timeToSend($desc['timetosend']*1000);
- 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->build();
- $sender = new Sender();
- $re = $sender->broadcastAll($message)->getRaw();
- //print_r($re);
- return true;
- }
- /*
- * 定时推送-部分用户
- */
- public static function androidPushPartTimingPush($desc)
- {
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
- $secret = $desc['secret'];
- $package = $desc['package'];
- Constants::setSecret($secret);
- Constants::setPackage($package);
- //Constants::setBundleId($package);
- $message = new Builder();
- $message->title($desc['title']); // 通知栏的title
- $message->description($desc['message']); // 通知栏的descption
- $message->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
- //$message->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
- //对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。
- $message->extra(Builder::notifyEffect, 0); // 此处设置预定义点击行为,1为打开app
- //$message->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
- $message->notifyType(1);//设置通知类型, type类型(-1, 1-使用默认提示音提示, 2-使用默认震动提示, 3-使用默认led灯光提示)
- //$message->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存
- $message->timeToSend($desc['timetosend']*1000);
- 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->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 androidPushAllTimingPushDelete($desc)
- {
- Constants::useOfficial();//正式环境
- //Constants::useSandbox();//测试环境
- $secret = $desc['secret'];
- $package = $desc['package'];
- Constants::setSecret($secret);
- Constants::setPackage($package);
- $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;
- }
- }
- }
- ?>
|