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

末鹿安然 提交于 2019-12-19 03:37:24

问题


Nowadays, PHP and Apple/iOS Push Notifications with Command 2 has been becoming popular. However not sure, how to prepare the format for same, as per Apple guideline here, How to achieve below packet format:

Also would like to know, how to receive Format of error-response packet as mentioned below:

At present, I am using below simple format:

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

with

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

回答1:


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

and for command 8 use this function: (by Yudmt) at 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);
        }
    }
}



回答2:


I believe that in building $msg you need

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

since Apple's documentation says the frame length should be 4 bytes

"Frame length - 4 bytes - The size of the frame data"

and for pack() 'n' generates 16 bits, while 'N' generates 32 bits

n - unsigned short (always 16 bit, big endian byte order)

N - unsigned long (always 32 bit, big endian byte order)



来源:https://stackoverflow.com/questions/21092378/php-apple-ios-push-notifications-command2-binary-interface-and-notification-f

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