Send Push Notification to User Devices - PHP

Source Code Option 1: cURL

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

$post_fields = array(
    "api_key" => "YOUR_API_KEY",    // Your App API Key
    "msg_title" => "My Push Notification",    // Message Title
    "user_id" => 12345,    // User ID
    "destination_url" => ""  // URL you want to open when user tap on notification.
);

$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

  
setUrl('https://send.pushnotificationapi.com/v1/push/send_by_userid');
$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',
  'user_id' => 'YOUR-CUSTOM-USER-ID',
  'msg_title' => 'test message',
  'destination_url' => 'https://pushnotificationapi.com'
));

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

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