我正在构建从服务器到 FCM 服务器的推送通知。我收到以下错误:Error=MissingRegistration
我的PHP代码如下。
function send($id,$title,$text){
$msg = [
'title' => $title,
'body' => $text,
'icon' => 'myicon',
'sound' => 'mySound'
];
$fields = [
'to' => $id,
'notification' => $msg
];
$headers = [
'Authorization: key=' . $api_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_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
echo $result;
}
我像这样调用函数:
send($id,$title,$text);
要向 FCM 发送数据,您需要创建 json 数据。请在字段后添加此行
$fields = json_encode($fields);