2012-09-01 52 views
3

我已將我的應用提交給蘋果商店,現在我想將APNS發送給用戶。 我想通過這個PHP代碼從我的桌面發送7000個推送消息:發送507推送消息後APNS關閉連接

<?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,然後它說:消息不是所有的休息交付 。

回答

3

問題是蘋果在X消息發送到不可用的設備ID之後關閉了連接。您必須通過使用apns反饋服務來清理deviceid的收集/數據庫,然後完成apns消息的部署。

相關問題