php - 无法从 Firebase 发送推送通知
问题描述
我想从网络向 Android 设备发送推送通知,但它响应“无效注册”。
我尝试从 firebase 控制台发送,但不包含消息数据
<?php
//$con= new mysqli("localhost","root","","doctor_appointment");
include('connection.php');
$sql = " Select * From token_register where id = 1";
$result = mysqli_query($connection_to_db,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["token"];
// $token = $row["token"];
// echo "$token";
}
}
mysqli_close($connection_to_db);
function send_notification ($tokens, $message)
{
// define( 'API_ACCESS_KEY', 'AIzaSyDt2xaRw4XGzghfxAMRFVy-I8ZeacBDbHA');
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => array($tokens),
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization:key = AIzaSyDdHTdvqzN8lrUqhZKOeuiR2d9cETRBhNw',
'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_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;
}
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
我希望是
{
"multicast_id":7068539387015084016,
"success":1,
"failure":0,
"results":[
{"RegistrationSuccess"}
]
}
但回应
{
"multicast_id":7068539387015084016,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{
"error":"InvalidRegistration"
}
]
}
解决方案
根据您给定的日志,注册 ID 是错误的。
尝试通过 Rest API 工具发送。例如邮递员。
推荐阅读
- c# - 对 Serilog 日志编写器的调用永远不会返回
- amazon-s3 - 当我上传到服务器时,表单边界被写入我的文件,使其损坏
- jmeter - 在 JMeter 中,如何根据从两个 jmeter 变量计算的值验证 json 中返回的值?
- java - 如何为 ListView 变体设置 OnClickListener?
- python - 在不规则多边形内生成随机点
- python - Python endwith() 函数,没有内置函数
- java - JComboBox,如果选中的项目发生变化或被选中,则触发一个动作
- angular - 具有动态输入的模板驱动表单的高级自定义验证器
- r - 如何在不等待响应的情况下发送 GET 请求
- javascript - 如何在语义 UI 反应中将状态传递给 Tab 渲染方法?