PHP iOS Group push
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, Stream_client_connect | Stream_client_persistent, $ctx);
//$fp = stream_socket_client (' ssl://gateway.push.apple.com:2195 ', $err, $errstr, stream_client_connect| Stream_client_persistent, $ctx);
if (! $fp) {
return false;
}
$payload =json_encode ($data);
$pushdata = chr (0). Pack (' n ', ' 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, 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);
}
}
------Solution--------------------
This kind of thing definitely needs to be implemented asynchronously.
------Solution--------------------
Try xmpphp, using a chat server to do it, maybe more reliable.
------Solution--------------------
It's going to be a lot faster.