How to Send the expo Notification using PHP CURL ?

In this tutorial we are going to learn the how to send expo mobile notification using PHP Curl.

there are some way to send the notification to user you can user the SDK. in this we are sending using the CURL.

Expo Notification

CURL function

function sendExpoMsg($arr, $endpoint){
	$post_json = json_encode($arr);
	$ch = @curl_init();
	@curl_setopt($ch, CURLOPT_POST, true);
	@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
	@curl_setopt($ch, CURLOPT_URL, $endpoint);
	@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
	@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = @curl_exec($ch);
	$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
	$curl_errors = curl_error($ch);
	@curl_close($ch);
	return $response;
}  

Send to user for single user :-

$arrayName = array ( 
	'to' => 'ExponentPushToken[********]',
	'title' => 'Title of the Notification',
	'body' => 'Body of your Notification',
);

Multiple users

$arrayName = array (
	array(
		'to' => 'ExponentPushToken[********]',
		'title' => 'Title of the Notification',
		'body' => 'Body of your Notification',
	),
	array(
		'to' => 'ExponentPushToken[********]',
		'title' => 'Title',
		'body' => 'Body Notification',
	),
);

Full code

<?php 
function sendExpoMsg($arr, $endpoint){
	$post_json = json_encode($arr);
	$ch = @curl_init();
	@curl_setopt($ch, CURLOPT_POST, true);
	@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
	@curl_setopt($ch, CURLOPT_URL, $endpoint);
	@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
	@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = @curl_exec($ch);
	$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
	$curl_errors = curl_error($ch);
	@curl_close($ch);
	return $response;
}  
$arrayName = array (
	array(
		'to' => 'ExponentPushToken[T6EefMOWQ8s-iNOJ2mQj_g]',
		'title' => 'Title of the Notification',
		'body' => 'Body of your Notification',
	),
	array(
		'to' => 'ExponentPushToken[T6EefMOWQ8s-iNOJ2mQj_g]',
		'title' => 'Title',
		'body' => 'Body Notification',
	),
);
echo $arr = json_encode($arrayName);
$url = 'https://exp.host/--/api/v2/push/send';
$result = sendExpoMsg($arrayName, $url);
print_r($result);
?>