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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值