Description of phpios group push: use php & nbsp; as the server side to push notifications to APNS of IOS. the code public & nbsp; static & nbsp; function & nbsp; apple_push ($ data, $ deviceToken) {$ passphrase123456; $ ckfileck. pem; $ ctx & nbsp; st php ios group push
Description: it uses php as the server side to push notifications to the APNS of IOS and attaches the code.
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);
}
There is no problem with single push
Problem description:
Currently, group push is required. Previously, I used to write data cyclically after using the socket link, and then disable fp. However, some people did not receive the push, if one of them has a problem with the deviceToken, the Apple server will interrupt the socket link.
I changed it to using socket to open, write, and close each user, but I found it was very slow, and 9000 messages could not be pushed in half an hour, although it was pushed in the background, however, it is required that the push should be completed within half an hour.
The method I first came up with is: Access This php page ten times at the same time, the parameter is not needed, and the user to be pushed will be divided into ten.
I don't know any good group push methods
Attach the current code
Function iospush ($ tid, $ subject, $ pushinfo ){
$ Data ['aps '] = array (
'Alert '=> $ subject,
'Sound' => 'default ',
'Type' => 'thread ',
'Tid' => $ tid,
);
$ Passphrase = '20140901 ';
$ 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 ['pythonetoken'];
$ Pushdata = chr (0 ). pack ('N', 32 ). pack ('H * ', $ deviceToken ). pack ('N', strlen ($ payload )). $ payload;
$ Fp = stream_socket_client ('SSL: // gateway.sandbox.push.apple.com: 000000', $ err, $ errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ ctx );
// $ Fp = stream_socket_client ('SSL: // gateway.push.apple.com: 000000', $ err, $ errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ ctx); // officially server
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 );
}
}
------ Solution --------------------
This must be implemented asynchronously.
------ Solution --------------------
Try xmpphp and use the chat server for implementation, which may be more reliable.
------ Solution --------------------
You can only start multiple processes as soon as possible.