About the apple Enhanced notification format

谁说我不能喝 提交于 2019-12-11 17:14:00

问题


I use php to send push message to apns , i use "Enhanced notification format" to sent .. but i can not get the return" Codes in error-response packet" anyone can help me ?? here is my code

<?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:


I call this function after fwrite and before close commands

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);
        }
    }
}

It works when I sending a notification in the enhanced format. But doesn't work with notification in the simple format. I don't have idea why... Any clue?



来源:https://stackoverflow.com/questions/10134749/about-the-apple-enhanced-notification-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!