发送推送到许多设备传递

时间:2013-05-07 06:55:11

标签: ios6 push-notification push apple-push-notifications passbook

我正在通过存折传递推文。推送工作,但没有显示推送通知,如果通过切换传递的背面静态更新,我可以得到更新的传递。这是我使用的代码:

<?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传递[21865]:对于pass.cam-mob.passbookpasstest太快推得太快 - 严格的速率限制将适用。


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

1 个答案:

答案 0 :(得分:1)

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

尝试在循环之前打开连接,将请求写入循环内的套接字,然后在脚本末尾关闭套接字。这将阻止你在一秒钟的时间内用几个请求快速攻击网关。