Description: Use PHP as server side, push notifications to iOS APNs, attach 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);}
Single push is no problem
Problem Description:
Now ask the group push, before, I wrote the socket link after the loop to write, and then close the FP, but some people did not receive the push, the internet said that if one of the people's mobile phone token (devicetoken) has a problem, the Apple server will interrupt the socket link.
I will be changed to each user with a socket open, write, close, but I found that this is very slow, half an hour can not push 9,000, although the background push, but the demand in half an hour push to finish.
I first think of the method is: at the same time to access the PHP page 10 times, parameters are not used, the user will be pushed into 10 parts.
Don't know what a good group push way
Attach the current code
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, 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);//Official 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);}}
Reply to discussion (solution)
This kind of thing definitely needs to be implemented asynchronously.
Try xmpphp, using a chat server to do it, maybe more reliable.
It's asynchronous, but every time it's turned on and off, it's a waste of time and can't be pushed out within half an hour.
With xmpphp It is not considered, since it is asynchronous push, without persistent connection.
Now my solution is: with PCNTL, open 10 Process execution
It's going to be a lot quicker.
Thank you for your answers, but when you use Pcntl, you encounter another problem.
http://bbs.csdn.net/topics/390829010