0

I tried executing the API APNS using CURL . The code is as below

$pem_file       =  'public/assets/pem/test.pem';
$pem_secret     = '';
$apns_topic     = 'com.test.demo';
if(defined('CURL_HTTP_VERSION_2_0'))
{
   $devicetoken=['54688ae0e1ecc3bc0d517521f4935c014342ecca792ef798f0e63652a4620ed4','de963dfbc03f5de416c7d806e04a6f3276716f246942fa2a61cffa393a780120','a08832de8b72567aa487beefe50e4e29caaa4a51ab14706fc8f0ef9df1f6b9c4','48335901c12bf02a03cef453d2e7739eaac9779991cafef3fa70ffe587ea3f12'];

$sample_alert = '{"aps":{"alert":"hi","sound":"default"}}';
   foreach($devicetoken as $device_token)
   {
    $url = "https://api.development.push.apple.com/3/device/$device_token";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
    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);
    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

//On successful response you should get true in the response and a status code of 200
     var_dump($response);echo "----";var_dump($httpcode);
    }
}
else{
   echo"NOt a supported version";
}

Iam getting the result as :

  �@@uUnexpected HTTP/1.x request: POST /3/device/54688ae0e1ecc3bc0d517521f4935c014342ecca792ef798f0e63652a4620ed4 bool(true) ----int(0)

I was trying this method to handle the invalid device token .Earlier i tried ssl using PHP like this link(http://learn-php-by-example.blogspot.in/2013/01/working-with-apple-push-notification.html) , In that when a invalid device token is in the db and when we pass it through the loop the push notification stops working . So i was trying the API method .

Thanks in advance for your response guys

Ranjith R
  • 13
  • 5

3 Answers3

1

for the new HTTP/2 APNS provider API, you can use curl to send push notifications : New APNS Provider API and PHP

  • I tried that link only , check the code i posted .Earlier i was using ssl that too i followed from a link mentioned . – Ranjith R Jul 28 '17 at 14:02
0

First make sure that your cURL version supports HTTP/2 (you'll have to compile cURL with this, it isn't active by default).

Then make sure cURL is using OpenSSL v1.0.2 or above.

Larcho
  • 536
  • 5
  • 10
0

Kindly enable your http2 in curl package of your php.

Also see the phpinfo() if you have already enabled the http2 support for curl or not.

After that just set your CURLOPT_HTTP_VERSION curl option with 3

enter image description here

Ashish Detroja
  • 1,084
  • 3
  • 10
  • 32