IOS通知批量内部服务器错误500

时间:2014-02-26 15:34:04

标签: php ios notifications push

目前,我们正试图通过UDID向我们的iOS开发人员发送推送通知。在测试以下脚本时,我收到了正确的通知,但在发送批量时(比方说2000),我们收到了内部服务器错误(500)消息。

我已经阅读了一些事情,例如发送到许多通知,管道导致Apple的关闭连接。

有谁知道我做错了什么?

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);
}

1 个答案:

答案 0 :(得分:1)

假设你有一个带设备ID的数组$ devices。您可以使用重复循环并包含睡眠功能:

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

但是,如果您实际上没有使用2000 不同的设备进行测试,Apple可能仍会阻止您的请求。