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.
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);
?>
Hello Friends, I’m Mohit Sharma funder of tutrialwala.com, Full Stack Web Developer. Expertise in WordPress, PHP, Laravel, Angular, and Frontend Development. In this publication, I share everything I know about PHP frameworks and Javascript frameworks, packages, and tools.
Have a project in mind? Get in touch