Critical result when sending a push notification from our server

We have an app in the appstore and with registered push notifications. They worked successfully all the time, but now we tried to send a "global" push and something strange happened. This is what we have in our server side .php file:

//Loop through tokens in tokenArray
$i = 0;
$t = 0;
foreach($tokenArray as $token)
{
    $t++;
    // Make notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;

    // Send
    $result;
    if($message != null)
    {
        $result = fwrite($fp, $msg, strlen($msg));
    }

if ($result)
    $i++;
}
// Close the connection to the server
fclose($fp);

if($i == 0)
{
    echo 'The message was not delivered to anyone out of '.$t.'.';
}
else
{
    echo 'The message was delivered to '.$i.' out of '.$t.'.';
}

      

The code before that always worked and it still does. TokenArray contains a table with tokens, as in SELECT Token FROM Tokens;

our SQL. It works.

During development, when only our own tokens were registered, he always said, "The message was delivered to 4 out of 4," even though we removed our apps from our phones. We have now tried to send all ≈1100 registered tokens using this code. The message was sent and the output was "The message was delivered on 588 of 1194." And we ourselves have not received the notification! What does it mean?

After about 5 minutes, I disabled the tokenArray with an array containing only my own tokens and sent a new push and I got it on my phone. I also know that the "working" token exists in the previous "tokenArray" which failed (I checked).

Is push notification a gamble !? What does it mean when if($result)

it fails? And why has it failed over 500 times?

All certificates and .pem and .p12 and everything works, the only thing I did from push1 to push2 was to use another table which is a clone from the original table in my SQL server. Table 2 only has my tokens and it worked. There were no other changes. Only SELECT Token FROM Tokens2

, and later I proved that all the tokens in Tokens2

exist in. Tokens

I have no idea if anyone got the push, or if the "lucky" 588 out of 1200 who still have the app installed got it.

What is causing this? We dare not send another one if half of them have already received it. Is there a limit to how quickly I can send rumors right away? Or what are we doing wrong ?! Please help, thanks.

+1


source to share


2 answers


Your main loop doesn't account for cases where Apple closes socket connections. As Eran mentioned, if you submit an invalid token, Apple closes the connection at this point, any further writes using fwrite will fail. Therefore, if your 589th token is invalid, no other push address will be sent to Apple.

Here's a simple fix for what fits into your logic; this part replaces the if statement in the main loop:

if ($result) {
    $i++;
} else {
    fclose($fp);
    // Add code here to re-open socket-connection with Apple.
}

      



Apart from the extended notification format mentioned by Eran, you can also use the APNS Feedback API to query Apple for invalid tokens and clear them from your database. You can find more details on this here: http://bit.ly/14RPux4

There is no limit to the number of push notifications you can send right away. I sent thousands in a few seconds. The only real limitation is the connection between you and the APNS servers.

+1


source


Well I don't know php so your code doesn't help me. However, based on your description, it is likely that some of the device tokens in your database are invalid. When the Apple server receives a notification with an invalid device token, it closes the socket. If you've already posted more messages after the bad token, they won't make it to Apple. Only after you find that the socket is closed and open a new one will your messages go to Apple. If you're not using the rich notification format, it would be a good idea to start using it - that way you can get the Invalid Message ID from Apple and clear your database of invalid tokens. However, even using the extended format does not guaranteethat you will find all errors (unless you want to send messages really slow and check Apple's error responses after every message you send).



+2


source







All Articles