六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1427|回复: 0

百度云推送服务器端(php版)

[复制链接]
 楼主| 发表于 2015-2-5 10:12:49 | 显示全部楼层 |阅读模式
百度云推送服务器端(php版)
  1. <?php
  2. namespace Admin\Controller;
  3. use Think\Controller;
  4. use  Org\Net;

  5. class AppController extends Controller{
  6.         private $appService;
  7.         private $appPush;
  8.         public function __construct(){
  9.                 parent::__construct();
  10.                 $this->appService = new \Common\Service\AppService();
  11.                 $this->appPush = new \Common\Service\AppService();
  12.        
  13.                
  14.         }
  15.        

  16.        
  17.         public function getpushList(){
  18.                 $itemList =$this->appService->getAllPush();
  19.                 $this->newsType = "app";
  20.                 $this->bgType = true;
  21.                 $Pager = new PageController();
  22.                  $page = I('page')?I('page'):1;
  23.                  $size =12;
  24.                  $pageArr = $Pager->pager($page,$size,$itemList);               
  25.                 $url = "http://".$_SERVER['HTTP_HOST']."/index.php/Admin/App/getpushList";               
  26.                  $this->pageShow = $Pager->pagerHtml($pageArr,$url);
  27.                 $this->appList =$pageArr[1];
  28.                 $this->display('Index/app/appPushList');
  29.         }
  30.         //新增推送
  31.         public function insertappPush(){
  32.                if(IS_GET){
  33.                          $this->newsType = "app";
  34.                                  $this->defaultUrl = C('defaultAppPushUrl');
  35.                          $this->display('Index/app/addAppPush');
  36.                    }else{
  37.                            $appPushInfo['title']=trim($_POST['title'])?trim($_POST['title']):C('defaultAppPushTitle');   
  38.                            $appPushInfo['description']=trim($_POST['description'])?trim($_POST['description']):C('defaultAppPushDescription');
  39.                $appPushInfo['url']=trim($_POST['url'])?trim($_POST['url']):C('defaultAppPushUrl');                          
  40.                           
  41.                $rec =$this->test_pushMessage_android('',$appPushInfo);         
  42.                            $appPushInfo['push_status'] =        $rec ;                  
  43.                            $appPushInfo['addTime']= time();
  44.                            $result = $this->appService->insertPush($appPushInfo);
  45.                                 if($result) {                  
  46.                                   $this->success('推送发送成功'.$rec,"http://" . $_SERVER["HTTP_HOST"] . "/index.php/Admin/App/getpushList");
  47.                                 }else{
  48.                                   $this->error('推送添加失败,请重新添加'.$rec);
  49.                                 }
  50.                         }
  51.          }
  52.          
  53.          //推送android设备消息
  54.         private function test_pushMessage_android ($user_id,$message_arr=array())
  55.         {  
  56.                 global $apiKey;
  57.                 global $secretKey;
  58.                 $apiKey=C('apiKey');               
  59.                 $secretKey=C('secretKey'); ;               
  60.                 $channel =  new \Org\Net\Channel( $apiKey, $secretKey ) ;
  61.                 //推送消息到某个user,设置push_type = 1;
  62.                 //推送消息到一个tag中的全部user,设置push_type = 2;
  63.                 //推送消息到该app中的全部user,设置push_type = 3;
  64.                 $push_type = 3; //推送单播消息
  65.                 //$optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user
  66.                 //optional[Channel::TAG_NAME] = "xxxx";  //如果推送tag消息,需要指定tag_name

  67.                 //指定发到android设备
  68.                 $optional[\Org\Net\Channel::DEVICE_TYPE] = 3;
  69.                 //指定消息类型为通知
  70.                 $optional[\Org\Net\Channel::MESSAGE_TYPE] = 1;
  71.                 //通知类型的内容必须按指定内容发送,示例如下:(open_type=2不跳转)
  72.                 $message = '{
  73.                                 "title": "'.($message_arr['title']).'",
  74.                                 "description": "'.($message_arr['description']).'",
  75.                                 "notification_basic_style":7,
  76.                                 "open_type":2,
  77.                                 "url":"'.($message_arr['url']).'",
  78.                 "custom_content": {
  79.                                         "key1":"value1",
  80.                                         "key2":"value2"}                                                        
  81.                         }';               
  82.                 $message_key = "msg_key";
  83.                 $ret = $channel->pushMessage ( $push_type, $message, $message_key, $optional ) ;
  84.                 if ( false === $ret )
  85.                 {          
  86.                    return  '失败';
  87.                 }
  88.                 else
  89.                 {
  90.                    return '成功' ;
  91.                 }
  92.     }

  93. }
复制代码
百度云推送服务器端(php版)
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表