Send Direct Push Notification - PHP

Source Code Option 1: cURL

  
                  $api_endpoint = "http://send.pushnotificationapi.com/v1/";
$api_method = "push/send_direct";

$post_fields = array(
    "api_key" => "YOUR_API_KEY",    // Your App API Key
    "device_token" => "XXXXXXXXXXXXXXXXXX",    // Device Token/Registration ID
    "platform" => "Android",    // Platform
    "msg_title" => "My Push Notification",    // Message Title
    "destination_url" => ""  // URL you want to open when user tap on notification (Optional)
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $api_endpoint . $api_method);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_fields));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// API response ....
echo $server_output;

 

Source Code Option 2: HttpRequest

  
                 <?php

$request = new HttpRequest();
$request->setUrl('https://send.pushnotificationapi.com/v1/push/send_direct');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/x-www-form-urlencoded'
));

$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
  'api_key' => 'YOUR-API-KEY',
  'device_token' => 'YOUR-DEVICE-TOKEN',
  'platform' => 'iOS',
  'msg_title' => 'test message',
  'destination_url' => 'https://pushnotificationapi.com'
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}