友盟推送,php简单代码
<?php /* 推送测试 */ namespace Service\Controller; use Think\Controller; class PushController extends Controller { private $_config = array(); public function __construct() { parent::__construct(); $this->_config = $this->_makeConfig(); } /* 安卓发送 */ public function PtoAndroid($device_tokens,$ticker,$title,$text) { if(count($device_tokens) > 500) { die(‘设备超过500个‘); } //拼接签名 $post_data = $this->_android($device_tokens,$ticker,$title,$text); $sign = $this->_makeSign($post_data,1); $url = $this->_config[‘url‘] . ‘?sign=‘ . $sign; //发送请求 $res = $this->_curl($url,$post_data); //判断 if($res[‘ret‘] != ‘SUCCESS‘){ //发送失败, die($res[‘data‘]->error_code); } } public function testA() { $device_tokens = array(‘AkFWCLpTmtJBeOf17rSnSAyPux4ayvzi6dru3O1avuSe‘); $this->PtoAndroid($device_tokens,‘通知栏的文字‘,‘测试标题‘,‘测试内容‘); } /* iOS发送 */ public function PtoIos($device_tokens,$text) { if(count($device_tokens) > 500) { die(‘设备超过500个‘); } //拼接post数据 $post_data = $this->_ios($device_tokens,$text); //拼接签名 $sign = $this->_makeSign($post_data,2); $url = $this->_config[‘url‘] . ‘?sign=‘ . $sign; //发送请求 $res = $this->_curl($url,$post_data); //判断 if($res[‘ret‘] != ‘SUCCESS‘){ //发送失败, die($res[‘data‘]->error_code); } } public function testIos() { $device_tokens = array(‘dd61807869efb4664491cd36eca8843a111a96d39e9772ea7940f0e83ce217c6‘); $this->PtoIos($device_tokens,‘hello‘); } //生成配置文件 private function _makeConfig() { return array( ‘method‘ => ‘POST‘, ‘url‘ => ‘http://msg.umeng.com/api/send‘, ‘ios_app_key‘ => ‘55541bb767e58e2094000492‘, ‘ios_app_master_secret‘ => ‘wbo2vnu5wmdvzfwzc2axdiiojj5ujkwt‘, ‘and_app_key‘ => ‘5551b48767e58ec961001d17‘, ‘and_app_master_secret‘ => ‘wjnbje5ju9rf89opo2p30phywwjqsavy‘, ); } /* 生成签名 @param post_body 请求体 type 1,安卓,2 ios @return str */ private function _makeSign($post_body,$type) { if($type == 1) return strtolower(md5($this->_config[‘method‘] . $this->_config[‘url‘] . $post_body . $this->_config[‘and_app_master_secret‘])); else return strtolower(md5($this->_config[‘method‘] . $this->_config[‘url‘] . $post_body . $this->_config[‘ios_app_master_secret‘])); } /* android post_body设置 @param ticker str 提示栏文字 title str 通知标题 text str 文字描述 device_tokens array 设备号 @return */ private function _android($device_tokens,$ticker,$title,$text) { $temp_arr = array( ‘appkey‘ => $this->_config[‘and_app_key‘], ‘timestamp‘ => time(), ‘type‘ => ‘listcast‘, ‘device_tokens‘ => trim(implode(‘,‘, $device_tokens),‘,‘), ‘payload‘ => array( ‘display_type‘ => ‘notification‘,//通知,友盟接管处理 ‘body‘ => array( ‘ticker‘ => $ticker, ‘title‘ => $title, ‘text‘ => $text, ‘after_open‘ => ‘go_custom‘, ‘custom‘ => ‘do things‘, //点击通知后做的事 ), ), ‘description‘ => ‘‘,//描述 ‘production_mode‘ => ‘false‘,//测试,上线为true ); return json_encode($temp_arr); } /* ios post_body消息体设置 @param ticker str 提示栏文字 title str 通知标题 text str 文字描述 device_tokens array 设备号 @return */ private function _ios($device_tokens,$text) { $temp_arr = array( ‘appkey‘ => $this->_config[‘ios_app_key‘], ‘timestamp‘ => time(), ‘type‘ => ‘listcast‘, ‘device_tokens‘ => trim(implode(‘,‘, $device_tokens),‘,‘), ‘payload‘ => array( ‘aps‘ => array( ‘alert‘ => $text, ‘after_open‘ => ‘go_custom‘, ‘custom‘ => ‘do things‘, //点击通知后做的事 ), ), ‘description‘ => ‘listcast‘,//描述 ‘production_mode‘ => ‘false‘,//测试,上线为true ); return json_encode($temp_arr); } /* 拼接curl请求 */ private function _curl($url,$post) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlErrNo = curl_errno($ch); $curlErr = curl_error($ch); curl_close($ch); if ($httpCode == "0") die("Curl error number:" . $curlErrNo . " , Curl error details:" . $curlErr . "\r\n"); else if ($httpCode != "200") die("Http code:" . $httpCode . " details:" . $result . "\r\n"); else return (array)json_decode($result); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。