PHP Apple APNS读取IMMEDIATE通知Status_code

FES*_*EST 2 php apple-push-notifications ios apns-php

在过去的3天里,我一直在尝试使用我的PHP代码和apns.

到目前为止,我能够发送通知(我只有一个设备,所以我不知道这是否适用于多个设备).

当我尝试测试为2个设备发送一个通知时,第一个是设备令牌无效(由我发明),第二个是我的设备令牌,我无法将通知发送到我的设备...从我读到的,当通知是不可读或错误的,apns关闭连接并发送6字节长的错误,其中第二个字节(status_code)是错误类型.如果没有错误发生,我仍然无法理解apns是否发送status_code 0(没有遇到错误)或者它是否根本不发送任何错误.

到目前为止,我无法找到这个status_code,尽管在互联网上发现了几个代码.

$ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', '.\certif\ck.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Create the payload body
    //file_put_contents('logs/debug_not.log', 'body'. "\n", FILE_APPEND);
    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default'
    );

    // Encode the payload as JSON
    //file_put_contents('logs/debug_not.log', 'json body'. "\n", FILE_APPEND);
    $payload = json_encode($body);
    $open = true;
    foreach ($devices as $device) {
        //file_put_contents('logs/debug_not.log', 'inicio ciclo'. "\n", FILE_APPEND);
        if($open == true){
            //file_put_contents('logs/debug_not.log', 'abrir ligacao'. "\n", FILE_APPEND);
            // Open a connection to the APNS server
            $fp = stream_socket_client(
                    'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

            if (!$fp){
               //file_put_contents('logs/debug_not.log', 'erro abrir ligacao'. "\n", FILE_APPEND);
               throw new \Exception;
            }
        }
        // Build the binary notification
        //file_put_contents('logs/debug_not.log', 'criar payload'. "\n", FILE_APPEND);
        $msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $device['token'])) . pack('n', strlen($payload)) . $payload;


        // Send it to the server
        //file_put_contents('logs/debug_not.log', 'enviar payload'. "\n", FILE_APPEND);
        $result = fwrite($fp, $msg, strlen($msg));
        stream_set_blocking ($fp, 0);    

        $errorResponse = @fread($apns, 6);
        //if i var_dump($errorResponse) it gives me null/false all the time, no matter what             

        //this is my workaround for invalid device tokens-- its not working or at least is not send the valid tokens
        if(!$result || !$fp)
        {

            //file_put_contents('logs/debug_not.log', 'erro na not '. chr($data["status_code"]). "\n", FILE_APPEND);
            fclose($fp);
            $open = true;
        } else{
            //file_put_contents('logs/debug_not.log', 'suc na not'. "\n", FILE_APPEND);
            $open = false;
        }
        //file_put_contents('logs/debug_not.log', 'fim ciclo'. "\n", FILE_APPEND);
    }
    // Close the connection to the server
    if($fp){
        //file_put_contents('logs/debug_not.log', 'fechar connection'. "\n", FILE_APPEND);
        fclose($fp);
    }
Run Code Online (Sandbox Code Playgroud)

我的连接上的fread始终为null/false,即使我收到通知或我正在尝试使用错误的令牌.

我错误后连接的逻辑似乎不起作用.

可以请有人帮帮我??? 我不打算使用第3类或代码,希望有一个简单的基本工作用于设备令牌数组,即使有一些apns给出错误.

感谢大家.

Pra*_*nki 6

你已经建立了联系 $fp = stream_socket_client ......

并且你正在尝试$errorResponse = @fread($apns, 6);哪些永远不会给你任何输出,因为没有定义$ apns.

相反,你应该这样做:

$errorResponse = @fread($fp, 6);
Run Code Online (Sandbox Code Playgroud)

并且您的代码将正常工作