发送推送通知时从APNS获取错误响应?

时间:2015-03-13 02:34:06

标签: ios notifications apple-push-notifications push response

我已使用此代码向iOS设备发送推送通知。当设备令牌正确时它工作得很好,我可以收到通知,APNS对我服务器的响应也没什么。但是当我将设备令牌更改为错误的令牌时,我仍然没有从APNS收到任何内容,响应中没有错误代码。我想在推送无效令牌时从APNS获得错误响应。

 <?php

error_reporting(0);
header("Content-type: application/json; charset=utf-8");
//include "dbconn.php";
$data = array();
$json= array();
$users = array();


$deviceToken = 'gddhfkgjhkjkgkgkgkgkjk2c2967b1fbc03fbebccb4109bbc509493938dcf70c3e674b949e1f81475ea742';
// Put your private key's passphrase here:
$passphrase = 'testpush1234';


// Put your alert message here:
$message = 'My first push notification!';

$token = $deviceToken;

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
# Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err,
                           $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);


 if (!$fp){
    echo "Error fp: ".$err;
    exit;
}

echo "\n".'Connected to APNS Push Notification' . PHP_EOL;

$body['aps'] = array(
                     'alert' => $message,
                     'sound' => 'default'
                     );

$payload = json_encode($body);
//echo "\n$payload" . PHP_EOL;

// Build the binary notification
$apple_expiry = time() + (90 * 24 * 60 * 60); 
$apple_identifier = 1;
$msg = pack("C", 1) . pack("N", $apple_identifier) . pack("N", $apple_expiry) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload; 

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

//set blocking
stream_set_blocking($fp,0);

//Check response
checkAppleErrorResponse($fp);

// Close the connection to the server
fclose($fp);

function checkAppleErrorResponse($fp) {

    $apple_error_response = fread($fp, 6); //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). Should return nothing if OK.
    //NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait forever when there is no response to be sent.

    echo 'apple_error_response ' . $apple_error_response;

    if ($apple_error_response) {

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

        echo "==============";
        echo $error_response;
        echo "==============";

        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';

        }

        echo '<br><b>+ + + + + + ERROR</b> Response Command:<b>' . $error_response['command'] . '</b>&nbsp;&nbsp;&nbsp;Identifier:<b>' . $error_response['identifier'] . '</b>&nbsp;&nbsp;&nbsp;Status:<b>' . $error_response['status_code'] . '</b><br>';
        echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';

        return true;
    }

    echo "\nPush respnse OK" . PHP_EOL;
    return false;
}

&GT;

0 个答案:

没有答案