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 '成功' ;
}
}
}