Home > Backend Development > PHP Tutorial > php ios 集体推送

php ios 集体推送

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:01:58
Original
1225 people have browsed it

php ios 群体推送
描述:用php 做服务器端,向IOS的APNS推送通知,附上代码

public static function apple_push($data,$deviceToken){<br />	$passphrase='123456';<br />	$ckfile="ck.pem";<br />	$ctx = stream_context_create();<br />	stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile);<br />	stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);<br />	$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);<br />	//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);<br /><br />	if(!$fp){<br />		return false;<br />	}<br />	<br />	$payload=json_encode($data);<br /><br />	$pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;<br />	$result = fwrite($fp, $pushdata, strlen($pushdata));<br />	if(!$result){<br />		file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken."-".microtime().PHP_EOL,FILE_APPEND);<br />	}<br />	<br />	fclose($fp);<br />}
Copy after login

单条推送是没有问题的
问题描述:
现在要求群体推送,之前,我写的是用socket链接后,循环写入,再关闭fp,但是有些人没有收到推送,网上说,要是其中一个人的手机令牌(deviceToken)有问题的话,苹果服务器就会中断这个socket链接。
我就改成每个用户都用socket打开,写入,关闭,但是我发现这样很慢,半个小时都不能推送9000条,虽然是后台推送,但是要求劲量在半小时内推送完。
我先在想到的办法是:同时访问这个php页面十次,参数不用,就将要推送的用户分成十份。
不知道有什么好的群体推送方式
附上现在的code
function iospush($tid,$subject,$pushinfo){<br />	$data['aps']=array(<br />		'alert'=>$subject,<br />		'sound'=>'default',<br />		'type'=>'thread',<br />		'tid'=>$tid,<br />	);<br />	$passphrase='123456';<br />	$ckfile="ck.pem";<br />	$ctx = stream_context_create();<br />	stream_context_set_option($ctx, 'ssl', 'local_cert', $ckfile);<br />	stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);<br />	<br />	$payload=json_encode($data);<br />	foreach($pushinfo as $info){<br />		$deviceToken=$info['phoneToken'];<br />		$pushdata = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;<br />		$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);<br />		//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);//正式服务器<br />		if(!$fp){<br />			file_put_contents(dirname(__FILE__)."/pushlog.txt",$err.'-'.$errstr.'-60'.PHP_EOL,FILE_APPEND);<br />			continue;<br />		}<br />		$result = fwrite($fp, $pushdata, strlen($pushdata));<br />		if(!$result){<br />			file_put_contents(dirname(__FILE__)."/pushlog.txt",$result.'-'.$deviceToken.PHP_EOL,FILE_APPEND);<br />		}<br />		fclose($fp);<br />	}<br />}
Copy after login

------解决方案--------------------
这种东西肯定需要用异步去实现
------解决方案--------------------
试试xmpphp,使用聊天服务器来实现,可能更靠谱一些。
------解决方案--------------------
要快就只能開多進程了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template