【问题标题】:About the apple Enhanced notification format关于苹果增强的通知格式
【发布时间】:2012-07-12 22:52:42
【问题描述】:

我使用 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(); */
}
?>

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 这只适用于增强格式的原因是因为错误在简单格式中被静默丢弃。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多