Php APNS不工作,SSL管道破裂&;错误写入重试

Php APNS不工作,SSL管道破裂&;错误写入重试,php,apple-push-notifications,Php,Apple Push Notifications,我正在为我的公司开发苹果应用程序 我正在服务器端使用PHP、laravel框架向APNS发送消息 对于测试,我使用硬代码设备令牌。 我让它在一个我硬编码的设备上工作 所以我先来看看发送apns的函数,一个在函数中请求的设备令牌。 所以当我调用这个函数时,它从数据库中获取设备令牌并发送到apns 我犯了一个错误 fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routine

我正在为我的公司开发苹果应用程序

我正在服务器端使用PHP、laravel框架向APNS发送消息

对于测试,我使用硬代码设备令牌。 我让它在一个我硬编码的设备上工作

所以我先来看看发送apns的函数,一个在函数中请求的设备令牌。 所以当我调用这个函数时,它从数据库中获取设备令牌并发送到apns

我犯了一个错误

fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry

我将代码更改回第一次测试,但它不像以前那样工作

有人知道我做错了什么,或者我的请求有错吗

请注意,这是我的代码

    $apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';

    $privateKeyPassword = 'my_private_key';

    $id = $request->input('id');
    $message = "message_here";

    $userSelected = User::select('device_token')->where('id', $id)->first();
    $deviceToken = str_replace(' ', '', $userSelected['device_token']);

    $pushCertAndKeyPemFile = $_SERVER['DOCUMENT_ROOT'].'my_certificate.pem';
    $stream = stream_context_create();
    stream_context_set_option($stream, 'ssl', 'passphrase', $privateKeyPassword);
    stream_context_set_option($stream, 'ssl', 'local_cert', $pushCertAndKeyPemFile);
    $connectionTimeout = 30;
    $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
    $connection = stream_socket_client($apnsServer, $errorNumber, $errorString, $connectionTimeout, $connectionType, $stream);

    if (!$connection){
        echo "Failed to connect to the APNS server. Error = $errorString <br/>"; 
        exit;
    }else{
        echo "Successfully connected to the APNS. Processing...</br>";
    }
    $messageBody['aps'] = array('alert' => $message, 'sound' => 'default', 'badge' => 1);

    $payload = json_encode($messageBody);
    $notification = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));

    if (!$wroteSuccessfully){
        echo "Could not send the message<br/>";
    } else {
        echo "Successfully sent the message<br/>"; 
    }
$apnsServer='1!'ssl://gateway.sandbox.push.apple.com:2195';
$privateKeyPassword='my_private_key';
$id=$request->input('id');
$message=“message\u here”;
$userSelected=User::select('device_token')->where('id',$id)->first();
$deviceToken=str_replace('',$userSelected['device_token']);
$pushCertAndKeyPemFile=$\服务器['DOCUMENT\u ROOT']。'my\u certificate.pem';
$stream=stream_context_create();
stream_context_set_选项($stream,'ssl','passphrase',$privateKeyPassword);
stream_context_set_选项($stream,'ssl','local_cert',$pushCertAndKeyPemFile);
$connectionTimeout=30;
$connectionType=STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
$connection=stream\u socket\u client($apnserver、$errorNumber、$errorString、$connectionTimeout、$connectionType、$stream);
如果(!$connection){
echo“无法连接到APNS服务器。错误=$errorString
”; 出口 }否则{ echo“已成功连接到APNS。正在处理…
”; } $messageBody['aps']=array('alert'=>$message,'sound'=>default','badge'=>1); $payload=json_encode($messageBody); $notification=chr(0)。包装('n',32)。包装('H*',$deviceToken)。组件('n',strlen($payload))$有效载荷; $wroteSuccessfully=fwrite($connection,$notification,strlen($notification)); 如果(!$wroteSuccessfully){ echo“无法发送消息
”; }否则{ echo“已成功发送消息
”; }
经过一些研究,我发现了我的问题

  • 当我们的服务器向APNS发送推送消息时,需要检查每个请求的响应错误。 检查错误后,我得到错误消息8,无效令牌

  • 当使用开发证书,并且有使用开发应用程序的用户注册时,他们获得设备令牌 但当您切换到生产时,您必须更改生产证书,注册用户需要获得新的设备令牌才能使推送通知工作

  • 因为您在开发应用程序中获得的设备令牌不能与生产证书一起使用

    错过重要的事情完全是我的错

        $apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';
    
        $privateKeyPassword = 'my_private_key';
    
        $id = $request->input('id');
        $message = "message_here";
    
        $userSelected = User::select('device_token')->where('id', $id)->first();
        $deviceToken = str_replace(' ', '', $userSelected['device_token']);
    
        $pushCertAndKeyPemFile = $_SERVER['DOCUMENT_ROOT'].'my_certificate.pem';
        $stream = stream_context_create();
        stream_context_set_option($stream, 'ssl', 'passphrase', $privateKeyPassword);
        stream_context_set_option($stream, 'ssl', 'local_cert', $pushCertAndKeyPemFile);
        $connectionTimeout = 30;
        $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
        $connection = stream_socket_client($apnsServer, $errorNumber, $errorString, $connectionTimeout, $connectionType, $stream);
    
        if (!$connection){
            echo "Failed to connect to the APNS server. Error = $errorString <br/>"; 
            exit;
        }else{
            echo "Successfully connected to the APNS. Processing...</br>";
        }
        $messageBody['aps'] = array('alert' => $message, 'sound' => 'default', 'badge' => 1);
    
        $payload = json_encode($messageBody);
        $notification = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
        $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
    
        if (!$wroteSuccessfully){
            echo "Could not send the message<br/>";
        } else {
            echo "Successfully sent the message<br/>"; 
        }