问题描述
我正在尝试使用Firebase云消息传递系统从服务器向我的android设备发送推送通知.我能够成功注册我的设备,并且也为我的设备生成了令牌.使用以下脚本
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.
谢谢
推荐答案
尝试使用收件人"而不是"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);
希望有帮助
这篇关于Android通过Firebase推送通知(服务器端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!