菜鸟教程小白 发表于 2022-12-12 19:31:11

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


                                            <p><p>目前,我们正在尝试使用他们的 UDID 向我们的 iOS 开发人员发送推送通知。在测试以下脚本时,我正确收到了通知,但是在发送批量(比如说 2000)时,我们收到了内部服务器错误 (500) 消息。 </p>

<p>我已经阅读了一些内容,例如向许多通知发送真正的管道,这会导致来自 Apple 的连接关闭。 </p>

<p>有人知道我做错了什么吗?</p>

<pre><code>include (&#39;functions/functions.php&#39;);

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

    // Provide the Certificate and Key Data.
    $tCert = &#39;pk.pem&#39;;

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

    // 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 &gt;=0).
    $tBadge = 1;

    // Audible Notification Option.
    $tSound = &#39;default&#39;;

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

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

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

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

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

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

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

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

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

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

    // Close the Connection to the Server.
    fclose ($tSocket);
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>假设您有一个带有设备 ID 的数组 $devices。您可以使用重复循环并包含 sleep 功能:</p>

<pre><code>$message = &#39;Nice to meet you.&#39;;
foreach ($devices as $deviceID) {
sendNotification($deviceID, $message);
usleep(500000);
}
</code></pre>

<p>但是,如果您实际上并未使用 2000 个<em>不同</em>设备进行测试,Apple 仍可能会阻止您的请求。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于php - IOS通知批量内部服务器错误500,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22046027/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22046027/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: php - IOS通知批量内部服务器错误500