APNS在发送507推送消息后关闭连接


APNS close connection after sending 507 push message

我已经将我的应用程序提交到苹果商店,现在我想向用户发送APNS。我想通过这个php代码从我的桌面发送7000条推送消息:

<?php
// Put your private key's passphrase here:
$passphrase = '*****';
// Put your alert message here:
$message = 'text here';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS</br>' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
// Encode the payload as JSON
$payload = json_encode($body);
////////////////////////////////////////////////////////////////////////
    function selectfromdb(){
    $x = array();
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("my_db", $con);
    mysql_query("set character_set_server='utf8'");
    mysql_query("set names 'utf8'");
    $result = mysql_query("SELECT idip FROM id");
    $c = 0;
    while($r = mysql_fetch_array($result))
    {
        $x[$c] = $r['idip'];
        $c++;
    }
    return $x;
}
///////////////////////////////////////////////////////////////////////
$y = selectfromdb();
$i = 0;
while ($i < sizeof($y)){
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*',$y[$i]) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered</br>' . PHP_EOL;
else
    echo 'Message successfully delivered</br>' . PHP_EOL;
    $i++;
}
echo 'end</br>';
// Close the connection to the server
fclose($fp);
?>

但它只发送了507,然后说:消息未送达剩下的都是。

问题是,苹果在向不可用的设备ID发送X条消息后关闭了连接。通过使用APNS反馈服务清理设备ID的集合/数据库,然后完成APNS消息的部署。