1. 程式人生 > >百度雲推送伺服器端(php版)

百度雲推送伺服器端(php版)

<?php
namespace Admin\Controller;
use Think\Controller;
use  Org\Net;

class AppController extends Controller{
	private $appService;
	private $appPush;
	public function __construct(){
		parent::__construct();
		$this->appService = new \Common\Service\AppService();
		$this->appPush = new \Common\Service\AppService();
	
		
	}
	

	
	public function getpushList(){
		$itemList =$this->appService->getAllPush();
		$this->newsType = "app";
		$this->bgType = true;
		$Pager = new PageController();
	 	$page = I('page')?I('page'):1;
	 	$size =12;
	 	$pageArr = $Pager->pager($page,$size,$itemList);		
		$url = "http://".$_SERVER['HTTP_HOST']."/index.php/Admin/App/getpushList";		
	 	$this->pageShow = $Pager->pagerHtml($pageArr,$url);
		$this->appList =$pageArr[1];
		$this->display('Index/app/appPushList');
	}
	//新增推送
	public function insertappPush(){
	       if(IS_GET){
		         $this->newsType = "app";
				 $this->defaultUrl = C('defaultAppPushUrl');
		         $this->display('Index/app/addAppPush');
		   }else{
			   $appPushInfo['title']=trim($_POST['title'])?trim($_POST['title']):C('defaultAppPushTitle');   
			   $appPushInfo['description']=trim($_POST['description'])?trim($_POST['description']):C('defaultAppPushDescription');
               $appPushInfo['url']=trim($_POST['url'])?trim($_POST['url']):C('defaultAppPushUrl');			   
			   
               $rec =$this->test_pushMessage_android('',$appPushInfo);	 
			   $appPushInfo['push_status'] =	$rec ;		   
			   $appPushInfo['addTime']= time();
			   $result = $this->appService->insertPush($appPushInfo);
				if($result) {		   
				  $this->success('推送傳送成功'.$rec,"http://" . $_SERVER["HTTP_HOST"] . "/index.php/Admin/App/getpushList");
				}else{
				  $this->error('推送新增失敗,請重新新增'.$rec);
				}
			}
	 }
	 
	 //推送android裝置訊息
	private function test_pushMessage_android ($user_id,$message_arr=array())
	{  
		global $apiKey;
		global $secretKey;
		$apiKey=C('apiKey');		
		$secretKey=C('secretKey'); ;		
		$channel =  new \Org\Net\Channel( $apiKey, $secretKey ) ;
		//推送訊息到某個user,設定push_type = 1; 
		//推送訊息到一個tag中的全部user,設定push_type = 2;
		//推送訊息到該app中的全部user,設定push_type = 3;
		$push_type = 3; //推送單播訊息
		//$optional[Channel::USER_ID] = $user_id; //如果推送單播訊息,需要指定user
		//optional[Channel::TAG_NAME] = "xxxx";  //如果推送tag訊息,需要指定tag_name

		//指定發到android裝置
		$optional[\Org\Net\Channel::DEVICE_TYPE] = 3;
		//指定訊息型別為通知
		$optional[\Org\Net\Channel::MESSAGE_TYPE] = 1;
		//通知型別的內容必須按指定內容傳送,示例如下:(open_type=2不跳轉)
		$message = '{ 
				"title": "'.($message_arr['title']).'",
				"description": "'.($message_arr['description']).'",
				"notification_basic_style":7,
				"open_type":2,
				"url":"'.($message_arr['url']).'",
                "custom_content": {
					"key1":"value1", 
					"key2":"value2"}			 				
			}';		
		$message_key = "msg_key";
		$ret = $channel->pushMessage ( $push_type, $message, $message_key, $optional ) ;
		if ( false === $ret )
		{	  
		   return  '失敗';
		}
		else
		{
		   return '成功' ;
		}
    }

}