class Um{ private $Android_appkey='549a24ebfd98c5cfeb000be5'; private $Android_appMasterSecret='z7fh3fqq6qn4eirat1h5dwagemyommpi'; private $Ios_appkey='549a2566fd98c5cfeb000d95'; private $Ios_appMasterSecret='fkpsmatbivamxnasubu5qw3dffbaaflo'; private $debug=FALSE; private $url= "http://msg.umeng.com/api/send"; function __construct() { if($this->debug) { ini_set('display_errors', TRUE); error_reporting(E_ALL); } } /* * 安卓推送 * ticker 必填 * title 必填 * text 必填 * description 可选 * play_vibrate 可选,震动,默认true * play_lights 可选,闪灯,默认true * play_sound 可选,声音,默认true * after_open 可选,点击动作, "go_app": 打开应用 "go_url": 跳转到URL "go_activity": 打开特定的activity * url 可选 当"after_open"为"go_url"时,必填。通知栏点击后跳转的URL,要求以http或者https开头 * activity 可选 * start_time 可选 * expire_time 可选 */ public function Android($params) { $play=array('true','false'); $after_open_array=array('go_app','go_url','go_activity'); if(!isset($params['ticker']) OR !$params['ticker']) { return array('code'=>0,'message'=>'缺失ticker字段'); } if(!isset($params['title']) OR !$params['title']) { return array('code'=>0,'message'=>'缺失title字段'); } if(!isset($params['text']) OR !$params['text']) { return array('code'=>0,'message'=>'缺失text字段'); } if(isset($params['play_vibrate']) AND in_array($params['play_vibrate'],$play)) { $play_vibrate=$params['play_vibrate']; }else{ $play_vibrate="true"; } if(isset($params['play_lights']) AND in_array($params['play_lights'],$play)) { $play_lights=$params['play_lights']; }else{ $play_lights="true"; } if(isset($params['play_sound']) AND in_array($params['play_sound'],$play)) { $play_sound=$params['play_sound']; }else{ $play_sound="true"; } if(isset($params['description'])) { $description=$params['description']; }else{ $description='友贷网-安卓消息推送'; } $data=array( "appkey"=>$this->Android_appkey, "timestamp"=>time(), "type"=>"broadcast", "payload"=>array( "display_type"=>"notification", // 通知,notification "body"=>array( "ticker"=>$params['ticker'], "title"=>$params['title'], "text"=>$params['text'], "play_vibrate" =>$play_vibrate, // 可选 收到通知是否震动,默认为"true". "play_lights" =>$play_lights, // 可选 收到通知是否闪灯,默认为"true" "play_sound" => $play_sound, // 可选 收到通知是否发出声音,默认为"true" "after_open" => "go_app", ) ), "description"=>$description ); //打开后的操作 if(isset($params['after_open']) AND in_array($params['after_open'],$after_open_array)) { $data['payload']['body']['after_open']=$params['after_open']; switch($params['after_open']) { case 'go_url': if(isset($params['url'])) { $data['payload']['body']['url']=$params['url']; }else{ return array('code'=>0,'message'=>'缺失url字段'); } break; case 'go_activity': if(isset($params['activity'])) { $data['payload']['body']['activity']=$params['activity']; }else{ return array('code'=>0,'message'=>'缺失activity字段'); } break; } }else{ $data['payload']['body']['after_open']='go_app'; } //是否为定时发送 if(isset($params['start_time']) AND ($params['start_time']>time())) { $data['policy']['start_time']=$params['start_time']; if(isset($params['expire_time']) AND ($params['expire_time']>$params['start_time'])) { $data['policy']['expire_time']=$params['expire_time']; } } //测试模式 if($this->debug) { $data['production_mode']='false'; $data['device_tokens']='AhHe8xPGoPInjKFnw-jwyXmDoL3iOrOqCfwc9n8mSzox'; } return $this->send($data,$this->Android_appMasterSecret); } public function Ios($params) { if(!isset($params['title']) OR !$params['title']) { return array('code'=>0,'message'=>'缺失title字段'); } if(isset($params['description'])) { $description=$params['description']; }else{ $description='友贷网-苹果消息推送'; } $data=array( "appkey"=>$this->Ios_appkey, "timestamp"=>time(), "type"=>"broadcast", "payload"=>array( "aps"=>array( "alert"=>$params['title'], ), "k1"=>"v1", // 自定义key-value "k2"=>"v2", ), "description"=>$description ); //是否为定时发送 if(isset($params['start_time']) AND ($params['start_time']>time())) { $data['policy']['start_time']=$params['start_time']; if(isset($params['expire_time']) AND ($params['expire_time']>$params['start_time'])) { $data['policy']['expire_time']=$params['expire_time']; } } //测试模式 if($this->debug) { $data['production_mode']='false'; $data['device_tokens']='ecf6ea33b2f9fec175f6ac52735bf5943d472af320f620db3782ccaeba990bb3'; } return $this->send($data,$this->Ios_appMasterSecret); } private function send($data,$Secret) { $postBody = json_encode($data); $sign = md5("POST" . $this->url . $postBody . $Secret); $url = $this->url . "?sign=" . $sign; $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, $postBody ); $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") { return array('code'=>$curlErrNo,'message'=> $curlErr); } else if ($httpCode != "200") { return array('code'=>$httpCode,'message'=> $result); } else { $result=json_decode($result,true); if(isset($result['ret']) AND $result['ret']=='SUCCESS') { return array('code'=>1,'message'=> $result['data']['task_id']); }else{ return array('code'=>0,'message'=> $result['data']['error_code']); } } }}