0

我正在为通过存折做推送通知。推送有效,但推送通知没有显示任何内容,如果通过切换通行证的背面进行静态更新,我可以获得更新的通行证。这是我使用的代码:

<?php


    // Provide the Certificate and Key Data.
    $cert = '../certificates/Certificates.pem';

    $payload = json_encode (array("aps" => ""));
    error_log('payload :'.$payload,0);


    // Create the and config the socket context. 
    $streamContext = stream_context_create ();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $cert);
    $password = ''; 

    if (strlen($password))
        stream_context_set_option($tContext, 'ssl', 'passphrase', $password);


    // Open the Connection to the APNS Server.
    $ConnectAPNS = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $streamContext);

$query1 = mysql_query("select deviceID from registration");
$row1 = mysql_fetch_array($query1);
$deviceID = $row1['deviceID'];

if(!empty($deviceID)){
$query2 = mysql_query("select pushToken from device ");

while($row2 = mysql_fetch_array($query2)){

    $pushToken= $row2['pushToken']; 

    // Compose and pack the push message
    $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $pushToken)) . chr(0) . chr(mb_strlen($payload)) . $payload;

    $success = fwrite($ConnectAPNS, $apns_message);

    // Check if we were able to open a socket.
    if ($success)
        error_log(date('d-m-Y hh:ii:ss', time()) . ': APNS Message successfully sent to push token ' . $pushToken, 0);
    else
        error_log(date('d-m-Y hh:ii:ss', time()) . ': Push error:' . $err . ': ' . $errstr);

    error_log('Stream Response: ' . print_r($success, true), 0); 
}
}
    // Close the Connection to the Server.
@socket_close($ConnectAPNS);
fclose ($ConnectAPNS);
include("feedback.php");
?>
`


通过使用此代码,我可以获得更新通行证的推送通知可能需要 30 毫秒!这是我得到的错误:`5 月 7 日 13:33:21 CamMobs-iPod4 passd[21865] :对于 pass.cam-mob.passbookpasstest 的推送太多太快——将应用严格的速率限制。


如何在没有此错误的情况下推送?

4

1 回答 1

1

每次迭代 while 循环时,您的代码都会打开一个新连接。

尝试在循环之前打开连接,将请求写入循环内的套接字,然后在脚本结束时关闭套接字。这将阻止您在一秒钟内通过多个请求快速攻击网关。

于 2013-05-07T09:13:44.457 回答