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

前端 未结 2 1887
醉话见心
醉话见心 2021-01-06 08:57

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, H

2条回答
  •  醉话见心
    2021-01-06 09:40

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

提交回复
热议问题