I am trying to send push notification to my android device from server using firebase cloud messaging system. I was able to successfully register my device and token for my device was generated as well. I am having trouble sending notification to my device using the below script
<?php
function send_notification($token1,$message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array('registration_ids'=>$token1,'data'=>$message);
$header = array('Authorization:key = <my key here>','Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result===false)
{
die('Curl failed'.curl_error($ch));
}
curl_close($ch);
return $result;
}
require "init.php"; //my db details here
$sql = "select token from fbuser";
$result = mysqli_query($con,$sql);
$tokens = array();
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
$tokens[] = $row["token"]; // i was able to successfully echo out token as well
}
}
mysqli_close($con);
$message = array("message"=> "This is a test message");
$message_status = send_notification($tokens,$message);
echo $message_status; ***//Trouble here. it just prints out "To" whenever i hit the php script***
?>
Now whenever i hit the script, the response just returns
To
and nothing else.. and No notification is delivered as well. Unable to figure out the issue. Requesting your guidance.
Thanks
Try to use 'to' instead of 'registration_ids'
Here is body data saved in postman history[i don't remember whether it works or not]:
{ "data": {
"score": "5x1",
"time": "15:10" }, "to" : "token_id"}
Check this script: If you want to do topic messaging
$url = "https://fcm.googleapis.com/fcm/send";
$topic = "topic_name";
$fields = array(
"to" => "/topics/".$topic,
"data" => $message,
"time_to_live" => 30,
"delay_while_idle" => true
);
$auth_key = "auth_key";
$headers = array(
"Authorization: key=".$auth_key,
"Content-Type: application/json"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
Hope that help
Thanks much @Shubham for the solution.
Guys please use @Shubham's code for sending notifications to "Topics'
I have modified his code a bit to address Single recipient messaging. Here it goes
<?php
$url = "https://fcm.googleapis.com/fcm/send";
$val = "<your recipient id>";
$message = array("message" => "This is a test message from server");
$fields = array(
"to" => $val,
"data" => $message,
"time_to_live" => 30,
"delay_while_idle" => true
);
$auth_key = "<Your auth key>";
$headers = array(
"Authorization: key=".$auth_key,"Content-Type: application/json"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
?>
to - Single recipient
registration_ids - multicast messages (Many recipients - should be in array)
This may not be the ideal solution from my end but it definitely works :)