APNS Push PHP wrong response
Asked Answered
L

1

11

We do have some issues with sending Pushes via APNS. As it seems we do get wrong responses from apple. The push is sent, but we do get an error response, which leads to pushes sent multiple times. Has anybody else experienced similar problems or do I miss something?

<?php
private function checkAppleErrorResponse($apns, PushToken $token)
{

    //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). Should return nothing if OK.
    $apple_error_response = fread($apns, 6);


    if ($apple_error_response) {
        //unpack the error response (first byte 'command" should always be 8)
        $error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response);

        if ($error_response['status_code'] == '0') {
            $error_response['status_code'] = '0-No errors encountered';
        } else if ($error_response['status_code'] == '1') {
            $error_response['status_code'] = '1-Processing error';
        } else if ($error_response['status_code'] == '2') {
            $error_response['status_code'] = '2-Missing device token';
        } else if ($error_response['status_code'] == '3') {
            $error_response['status_code'] = '3-Missing topic';
        } else if ($error_response['status_code'] == '4') {
            $error_response['status_code'] = '4-Missing payload';
        } else if ($error_response['status_code'] == '5') {
            $error_response['status_code'] = '5-Invalid token size';
        } else if ($error_response['status_code'] == '6') {
            $error_response['status_code'] = '6-Invalid topic size';
        } else if ($error_response['status_code'] == '7') {
            $error_response['status_code'] = '7-Invalid payload size';
        } else if ($error_response['status_code'] == '8') {
            $error_response['status_code'] = '8-Invalid token';
        } else if ($error_response['status_code'] == '255') {
            $error_response['status_code'] = '255-None (unknown)';
        } else {
            $error_response['status_code'] = $error_response['status_code'] . '-Not listed';
        }

        if ($token->isExistingUser()) {
            $this->logger->error('#cloudalarm User-Id: ' . $token->getAssociatedUser() . ': Something went wrong with APNS pushing , error is ' . $error_response['status_code']);
        } else {
            $this->logger->error('#cloudalarm  for simple login id ' . $token->getAssociatedUser() . ': Something went wrong with APNS pushing , error is ' . $error_response['status_code']);
        }

        return true;
    }

    return false;
}
?>
Languor answered 5/5, 2017 at 16:47 Comment(7)
Which error are you receiving? Please include that in your question.Watt
@Watt the only error we see in our logs is '8-Invalid token', which is a valid response in some casesLanguor
So you believe the token is correct but the error says it is not? How do you verify the tokens actually work?Chastise
Not strictly an answer, but I would consider using switch and case instead of all of those else ifs, it'd be easier to maintain. However, are you trying to contact the APNS server using sandbox tokens? If that's the case then you will receive that error. You may also need to republish the production certificate as the private key might have become bad?Oscular
@DanFromGermany We rely on the response we do get in the stream. We are pushing to production, the thing is : the notifications are getting sent (received on actual devices), but we do get a an error response.Languor
please read this #14925858Contortion
Could you have had a confusion between development and production environment (pem certificate on the server and device apns token) ? This happens so often…Marmalade
J
1

I think you are misunderstanding the error. Typically the response codes are meant to help you manage your list. If you get an "invalid token" error it is typically accompanied by an updated token, remove the invalid one and insert the new one in it's place. Keep in mind that APNS does 'forward' the message automatically and the notice is just to help you stay up to date.

If you are not getting the updated token then it could be that your app was deleted and not reinstalled so the token is simply invalid.

Jurisprudent answered 15/5, 2017 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.