Q:

Iphone push notification service problem

I am using Push Notification in my app and a PHP server that manages database with tokens and sending payload to Apple servers.  Sending messages to a small number of devices (I have tried with 2) works well but when I want to send a message to entire database ( over 20.000 devices ) it doesn't work.  Connection with Apple server is made ( i get no errors of connection ) but the two devices I have ( that are also in database ) do not received the message.  
   The PHP code is:
$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'path/to/my/certificate.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' .  $apnsHost . ':' .  $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

while($row = mysql_fetch_array($result))
{
    $deviceToken=$row['token'];

    $apnsMessage = chr(0) .  chr(0) .  chr(32) .  pack('H*', str_replace(' ', '', $deviceToken)) .  chr(0) .  chr(strlen($payload)) .  $payload;
    fwrite($apns, $apnsMessage);

    if ($error==0) 
    {
        echo "Notification was sent successfully to ".$row['token']."<br/>";
    }
    else
    {
        echo "Notification failure to ".$row['token']."<br/>";
    }
}

socket_close($apns);
fclose($apns);

As a result I am getting "Notification was sent successfully" for all the records in database but it seems like it doesn't send the message because I don't see it on my devices.  When I am using the same code to send message to 2 tokens it works well.  What could be the problem?  Is there an upper limit for number of devices I can send to with one connection?  

A:

Not sure if there is a limit, but I'm sure it won't be low as I have successfully sent notifications to thousands of devices at a time in the past (although currently I'm shooting them about 250-500 at a time). 
Try replacing this:
$apnsMessage = chr(0) .  chr(0) .  chr(32) .  pack('H*', str_replace(' ', '', $deviceToken)) .  chr(0) .  chr(strlen($payload)) .  $payload;

With this:
$apnsMessage = chr(0) .  pack("n",32) .  pack('H*', str_replace(' ', '', $deviceToken)) .  pack("n", strlen($payload)) .  $payload;

Also, you might be interested in trying this out: http://code.google.com/p/apns-php/

