关于apple增强型通知格式

时间:2012-04-13 03:12:28

标签: php apple-push-notifications apns-php

我使用php向apns发送推送消息,我使用“增强通知格式”发送..但我无法获得返回“错误响应包中的代码”任何人都可以帮助我?这是我的代码

<?php
header("Content-Type: text/html; charset=UTF-8");

$deviceToken = "123";

$content = "testing";

if(isset($content))

{

$newContent=substr($content,0,30)."...";

$re_content=iconv("GB2312","UTF-8",$newContent);
$pass = 'Ladder';

$body = array("aps" => array("alert" => $re_content, "badge" => 1, "sound" => 'received5.caf'));

            $ctx = stream_context_create();

            stream_context_set_option($ctx, 'ssl', 'local_cert', 'dev.pem');

            stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);

            //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

            $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

            stream_set_blocking ($fp, 0);
            if (!$fp) 
            {
                print "Failed to connect $err $errstrn";
                return;
            }
            else
            {
            print "Connection OK\n<br/>";
            }


            $payload = json_encode($body);


            $msg = 
            // new: Command "1"
            chr(1)
            // new: Identifier "1111"
            . chr(1) . chr(1) . chr(1) . chr(1)
            // new: Expiry "tomorrow"
            . pack('N', time() + 86400)
            // old 
            . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
            //print "sending message :" . $payload . "\n";
            fwrite($fp, $msg);
            //checkAppleErrorResponse($fp);
            echo 'Done\n';

            fclose($fp);

            echo $apple_error_response = fread($fp, 6);

            /* return false;
            exit(); */
}
?>

1 个答案:

答案 0 :(得分:5)

我在fwrite之后和关闭命令之前调用此函数

function error_response($fp)
{
    $read = array($fp);
    $null = null;
    $changedStreams = stream_select($read, $null, $null, 0, 1000000);

    if ($changedStreams === false)
    {
        echo ("Error: Unabled to wait for a stream availability");
    }
    elseif ($changedStreams > 0)
    {
        $responseBinary = fread($fp, 6);
        if ($responseBinary !== false || strlen($responseBinary) == 6)
        {
            $response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary);
            var_dump($response);
        }
    }
}

当我以增强格式发送通知时,它可以正常工作。但不适用于简单格式的通知。我不知道为什么......有任何线索?