我正在尝试使用firebase云消息系统从服务器向我的Android设备发送推送通知。我能够成功注册我的设备,并且我的设备也生成了令牌。我无法使用以下脚本向我的设备发送通知
<?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***
?>
现在每当我点击脚本时,响应都会返回
To
,没有别的......也没有提供任何通知。无法弄清楚问题。请求你的指导。
由于
答案 0 :(得分:1)
尝试使用'to'代替'registration_ids'
这是保存在邮递员历史中的身体数据[我不记得它是否有效]:
{ "data": {
"score": "5x1",
"time": "15:10" }, "to" : "token_id"}
检查此脚本:如果要进行主题消息传递
$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);
希望有帮助
答案 1 :(得分:0)
非常感谢@Shubham的解决方案。
伙计们请使用@ Shubham的代码向“话题”发送通知
我已经修改了他的代码以解决单个收件人消息传递问题。在这里
<?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 - 单一收件人
registration_ids - 多播消息(许多收件人 - 应该在数组中)
这可能不是我最终的理想解决方案,但绝对有效:)