关于苹果增强通知格式 [英] About the apple Enhanced notification format

查看:29
本文介绍了关于苹果增强通知格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我在fwrite之后和close命令之前调用这个函数

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?

这篇关于关于苹果增强通知格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆