【发布时间】:2021-12-31 13:07:32
【问题描述】:
我正在尝试从 PHP 发送推送消息,但收到以下错误消息:
{"multicast_id":4832091122368281316,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
我认为浏览器 ID 不匹配,但我不知道应该使用哪个。
当我通过 JS 注册推送通知时,我在我的 PHP 服务器上收到以下负载:
{"endpoint":"https://fcm.googleapis.com/fcm/send/XXX:YYY","expirationTime":null,"keys":{"p256dh":"ZZZ","auth" :"AAA"}}
为了在 PHP 中发送消息,我使用了以下代码(在 stackexchange 上找到):
function sendnotification($to, $title, $message, $img = "", $datapayload = "")
{
$msg = urlencode($message);
$data = array(
'title'=>$title,
'sound' => "default",
'msg'=>$msg,
'data'=>$datapayload,
'body'=>$message,
'color' => "#79bc64"
);
if($img)
{
$data["image"] = $img;
$data["style"] = "picture";
$data["picture"] = $img;
}
$fields = array(
'to'=>$to,
'notification'=>$data,
'data'=>$datapayload,
"priority" => "high",
);
$headers = array(
'Authorization: key=MYSERVERKEY',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
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);
curl_close( $ch );
return $result;
我认为我使用了正确的服务器密钥,如果不正确,我会收到不同的错误消息。
对于 $to,我想知道我应该使用什么。我以为我必须在端点之后使用 XXX:YYY 但它不起作用(XXX 很短,YYY 很长)。我也试过 ZZZ 和 AAA,但也没有用。
我的问题:
我应该使用哪种 ID 将消息发送到特定浏览器?
我应该怎么做才能向所有注册的浏览器发送通知?
谢谢!
【问题讨论】:
标签: php push-notification firebase-cloud-messaging