<?php
class helper_push{
const MAXTIMES = 3;
static private $_times = 0;
private $fp;
const APIKEY = '';
const SECRETKEY = '';
const PWD = '';
public function __construct(){
}
public function getLink(){
$passphrase = PWD;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', DEDEDATA.'/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){//连接失败
if(self::$_times < self::MAXTIMES){//失败次数小于5次,再次实例化
self::$_times += 1;
return $this->getLink();
}else{//失败次数大于5次,记录操作失败记录
return false;
}
}else{//连接成功
$this->fp = $fp;
return true;
}
}
public function closefp(){
fclose($this->fp);
}
public function setPush($deviceToken,$message){
if($this->is_serialized($deviceToken)){
$user_id = $channel_id = '';
$dt_arr = unserialize($deviceToken);
$user_id = $dt_arr['user_id'];
$channel_id = $dt_arr['channel_id'];
$this->setAndroidPush($user_id,$channel_id,$message);
unset($user_id,$channel_id,$dt_arr);
}else{
$this->setIOSPush($deviceToken, $message);
}
}
private function setIOSPush($deviceToken,$message){
$body['aps'] = $message;
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($this->fp, $msg, strlen($msg));
if (!$result){//发送失败
if(self::$_times < self::MAXTIMES){//失败次数小于5次,再次发送
self::$_times += 1;
$this->setPush($deviceToken, $message);
}else{//失败次数大于5次,记录操作失败记录
return false;
}
}else{//发送成功
return true;
}
}
/**
*
* 已广播的形式发送推送通知
* @param array $message_arr
*/
public function a_broadcastPush($message_arr){
require_once ( "Channel.class.php" ) ;
$channel = new Channel(self::APIKEY,self::SECRETKEY);
$push_type = 3;
$optional[Channel::MESSAGE_TYPE] = 1;
$message_str = '"description":"'.$message_arr['alert'].'",';
unset($message_arr['alert'],$message_arr['badge'],$message_arr['sound']);
$custom_content = '';
foreach ($message_arr as $key=>$value){
$custom_content .= '"'.$key.'":"'.$value.'",';
}
$custom_content = trim($custom_content,',');
$message = '{
"title": "'.diconv('中文', 'gbk', 'utf-8').'",
'.$message_str.'
"custom_content":{'.$custom_content.'},
"notification_basic_style":7,
"open_type":0
}';
unset($message_str,$custom_content);
$message_key = "msg_key";
$ret = $channel->pushMessage ( $push_type, $message, $message_key,$optional ) ;
return $ret;
}
private function setAndroidPush($user_id,$channel_id,$message_arr){
require_once ( "Channel.class.php" ) ;
$channel = new Channel(self::APIKEY,self::SECRETKEY);
$push_type = 1;
$optional[Channel::USER_ID] = $user_id;
$optional[Channel::CHANNEL_ID] = $channel_id;
$optional[Channel::DEVICE_TYPE] = 3;
$optional[Channel::MESSAGE_TYPE] = 1;
$message_str = '"description":"'.$message_arr['alert'].'",';
unset($message_arr['alert'],$message_arr['badge'],$message_arr['sound']);
$custom_content = '';
foreach ($message_arr as $key=>$value){
$custom_content .= '"'.$key.'":"'.$value.'",';
}
$custom_content = trim($custom_content,',');
$message = '{
"title": "'.diconv('中文', 'gbk', 'utf-8').'",
'.$message_str.'
"custom_content":{'.$custom_content.'},
"notification_basic_style":7,
"open_type":0
}';
unset($message_str,$custom_content);
$message_key = "msg_key";
$ret = $channel->pushMessage ( $push_type, $message, $message_key, $optional ) ;
if ( false === $ret ){
// file_put_contents(dirname(__FILE__).'\push.log','Message not delivered, user_id:'.$user_id.' and channel_id:'.$channel_id.' and msg:'.$message,FILE_APPEND);
}else{
// file_put_contents(dirname(__FILE__).'\push.log','Message successfully delivered, user_id:'.$user_id.' and channel_id:'.$channel_id.' and msg:'.$message,FILE_APPEND);
}
}
private function is_serialized($data){
$data = trim( $data );
if ( 'N;' == $data )
return true;
if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
return false;
switch ( $badions[1] ) {
case 'a' :
case 'O' :
case 's' :
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
return true;
break;
case 'b' :
case 'i' :
case 'd' :
if ( preg_match( "/^{$badions[1]}:[0- E-]+;\$/", $data ) )
return true;
break;
}
return false;
}
}
?>
|