PHP Apple iOS推送通知:Command2:二进制接口和通知格式

时间:2014-01-13 13:21:41

标签: php ios binary format apple-push-notifications

如今,使用Command 2的PHP和Apple / iOS推送通知已经变得越来越流行。但是不确定,如何根据Apple guideline here准备相同的格式,如何实现以下数据包格式:

PHP iOS Push Notifications: Command2 : Binary Interface and Notification Format

还想知道如何接收Format of error-response packet,如下所述:

Format of error-response packet

目前,我使用的是以下简单格式:

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

fwrite($fp, $msg, strlen($msg));

2 个答案:

答案 0 :(得分:2)

//command 2
$msgInner =
  chr(1)
. pack('n', 32)
. pack('H*', $deviceToken)

. chr(2)
. pack('n', strlen($payload))
. $payload

. chr(3)
. pack('n', 4)
. chr(1).chr(1).chr(1).chr(1)

. chr(4)
. pack('n', 4)
. pack('N', time() + 86400)

. chr(5)
. pack('n', 1)
. chr(10);

$msg=
chr(2)
. pack('N', strlen($msgInner))
. $msgInner;

和命令8使用此函数:(由Yudmt)About the apple Enhanced notification format

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

答案 1 :(得分:1)

我相信在构建$ msg时你需要

. pack('N', strlen($msgInner))

因为Apple的文档说帧长度应该是4个字节

  

“帧长度 - 4个字节 - 帧数据的大小”

pack()'n'生成16位,而'N'生成32位

  

n - 无符号短(始终为16位,大端字节顺序)

     

N - 无符号长整数(总是32位,大端字节顺序)