foreach ($users AS $userToken) {
$msg = chr(0) . pack('n', 32) . pack('H*', $userToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
}
For reference, $fp
is a 'PHP Stream Context'. $users
is an array of user tokens that have registered for push notifications.
Is there a better way of writing the above code so that fwrite can somehow send an array of messages to APNS rather than writing to the socket for each individual user?