2014-02-26 46 views
2

目前我們正在嘗試通過其UDID發送推送通知給我們的iOS開發人員。在測試下面的腳本時,我正確地收到了通知,但是當發送一個批量文件(比如說2000)時,我們收到了內部服務器錯誤(500)消息。IOS通知批量內部服務器錯誤500

我讀過一些東西,比如發送到很多通知,真正的導致蘋果關閉連接的管道。

有誰知道我在做什麼錯?

include ('functions/functions.php'); 

function sendNotification($deviceID, $message) 
{ 
    // Provide the Host Information. 
    $tHost = 'gateway.push.apple.com'; 
    $tPort = 2195; 

    // Provide the Certificate and Key Data. 
    $tCert = 'pk.pem'; 

    // Provide the Private Key Passphrase (alternatively you can keep this secrete 
    // and enter the key manually on the terminal -> remove relevant line from code). 
    // Replace XXXXX with your Passphrase 
    $tPassphrase = 'xxx'; 

    // Provide the Device Identifier (Ensure that the Identifier does not have spaces in it). 
    // Replace this token with the token of the iOS device that is to receive the notification. 
    //$tToken = $value; 

    // The message that is to appear on the dialog. 
    $tAlert = $message; 

    // The Badge Number for the Application Icon (integer >=0). 
    $tBadge = 1; 

    // Audible Notification Option. 
    $tSound = 'default'; 

    // The content that is returned by the LiveCode "pushNotificationReceived" message. 
    $tPayload = $message; 

    // Create the message content that is to be sent to the device. 
    $tBody['aps'] = array (
     'alert' => $tAlert, 
     'badge' => $tBadge, 
     'sound' => $tSound, 
     ); 
    $tBody ['payload'] = $tPayload; 

    // Encode the body to JSON. 
    $tBody = json_encode ($tBody); 

    // Create the Socket Stream. 
    $tContext = stream_context_create(); 
    stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert); 

    // Remove this line if you would like to enter the Private Key Passphrase manually. 
    stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase); 

    // Open the Connection to the APNS Server. 
    $tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext); 

    // Check if we were able to open a socket. 
    if (!$tSocket) 
     exit ("APNS Connection Failed: $error $errstr" . PHP_EOL); 

    // Build the Binary Notification. 
    $tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $deviceID) . pack ('n', strlen ($tBody)) . $tBody; 

    // Send the Notification to the Server. 
    $tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));  

    /*if ($tResult) 
     echo 'Delivered Message to APNS' . PHP_EOL; 
    else 
     echo 'Could not Deliver Message to APNS' . PHP_EOL;*/ 

    // Close the Connection to the Server. 
    fclose ($tSocket); 
} 
+0

看來你打開/關閉每個通知服務器的SSL連接 - 這不是一個好主意。嘗試在單個連接內發送所有通知 - 只需在每次傳輸之間放置10ms左右的睡眠時間。 –

+0

@rokjarc感謝您的回答,您可以告訴如何在代碼中添加睡眠以及如何進行單一連接? – Babidi

+0

@ user3355847你真的用2000個不同的設備進行測試嗎?如果沒有,蘋果可能會阻止你,因爲你試圖經常聯繫同一個設備。要使PHP腳本等待500毫秒,請使用usleep(500000)。 – Mark

回答

1

我們假設你有一個帶有設備ID的數組$ device。您可以使用重複循環,包括睡眠功能:

$message = 'Nice to meet you.'; 
foreach ($devices as $deviceID) { 
    sendNotification($deviceID, $message); 
    usleep(500000); 
} 

但是,如果你沒有什麼實際的2000 不同設備上測試,蘋果仍然可能會阻止您的要求。