APNs Provider API batch request APNs Provider API batch request curl curl

APNs Provider API batch request


Not sure on curl, but in general, Apns providers must maintain persistent connections toward Apns Cloud. There is no way to broadcast to multiple devices using single message. Apns providers should take leverage of http/2 (multiple streams per connection) and can also send messages across multiple connections, but must not do connection and disconnection in loop that would be treated as DoS attack.

Avoid the connection loop, you should post the messages in loop, connection/disconnection part must not be part of loop.

I hope it helps.

Regards,_Ayush


libcurl automatically attempts to keep a connection open whenever possible. The pattern to follow is to do the following:

1) Create the curl handle: $ch = curl_init();

2) Set appropriate options on the handle:

curl_setopt($ch, CURLOPT_POSTFIELDS, '{"aps":{"alert":"Here I am","sound":"default"}}');curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);

3) Begin the for loop, set the url for each recipient and execute the request:

for ($tokens as $token) { //Iterate push tokens    $url = "https://api.development.push.apple.com/3/device/{$token}";    curl_setopt($ch, CURLOPT_URL, $url);    $result = curl_exec($ch);    // Check result, handle errors etc...   }   curl_close($ch); // Close connection outside the loop

Following the approach above should keep the connection open and comply with Apple's requirements.