To say the good thing first - my code is working. But unfortunately, it is extremly slow with many devices to push to. As the whole pushing process failed when one device failed with my code before (the connection was made outside the loop), I took the connection inside my loop:
foreach ($deviceTokens as $token) {
$fp = stream_socket_client($this->data['config']['push']['apnsAddress'], $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
$msg = chr(0).pack('n', 32).pack('H*', $token).pack('n', strlen($payload)).$payload;
$result = fwrite($fp, $msg);
if (!$result) {
$this->pushLog('devicetoken '.$token.' failed');
$errors++;
}
fclose($fp);
}
Isn't there any other way to push to multiple devices than making a new connection for every single person that has an app installed? The runtime for this script is just way to long at the moment.