时间:2021-07-01 10:21:17 帮助过:12人阅读
public static function apple_push($data,$deviceToken){
$passphrase='123456';
$ckfile="ck.pem";
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if(!$fp){
return false;
}
$payload=json_encode($data);
$pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $pushdata, strlen($pushdata));
if(!$result){
file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken."-".microtime().PHP_EOL,FILE_APPEND);
}
fclose($fp);
}
function iospush($tid,$subject,$pushinfo){
$data['aps']=array(
'alert'=>$subject,
'sound'=>'default',
'type'=>'thread',
'tid'=>$tid,
);
$passphrase='123456';
$ckfile="ck.pem";
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$payload=json_encode($data);
foreach($pushinfo as $info){
$deviceToken=$info['phoneToken'];
$pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);//正式服务器
if(!$fp){
file_put_contents(dirname(__FILE__)."/pushlog.txt",$err.'-'.$errstr.'-60'.PHP_EOL,FILE_APPEND);
continue;
}
$result = fwrite($fp, $pushdata, strlen($pushdata));
if(!$result){
file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken.PHP_EOL,FILE_APPEND);
}
fclose($fp);
}
}