3

我已将我的应用程序提交到苹果商店,现在我想将 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,然后它说:消息未传递给所有其余部分。

4

1 回答 1

3

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

于 2013-01-11T14:55:13.710 回答