我有fcm server.Build在Laravel流明。
服务器网址:https://firenoti.000webhostapp.com/fcmserver/public/api/noti
发布请求:title=Hi&token=f12S4al_8dg:APA91bFuNy4SrMOH8AiK0M_AGd8SUIraegqYEPgffdBY6AujN6b84ALtM22W9mAocuBq5bf83mHqOPPP1T-OSW0rAVMUVIOQkDxEd9l-MoQ3kaNrv4d4Q5pOsl5qKahwx_55FZ-Gf9gV&body=Hello
我使用HttpRequester进行帖子请求。 这是我的服务器代码:
public function getData(Request $request)
{
$firebase_url = 'https://fcm.googleapis.com/fcm/send';
$api_key = ”api_key”;
if ($request->has('token') && $request->has('token') && $request->has('token')) {
$token = $request->get('token');
$title = $request->get('title');
$body = $request->get('body');
if($token == "ALL"){
$token = "/topics/all";
}
$fields = ['to' => $token,'data'=> array('title' => $title, 'body' => $body)];
$headers = ['Content-type : application/json; charset=utf-8"',"Authorization:key = ".$api_key];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $firebase_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_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
$result_array = json_decode($result,true);
if ($result_array['success'] == 1) {
$status = "OK";
$res_message = "Notification send successfuly";
}else{
$status = "ERROR";
$res_message = "Notification sending fail";
}
return response()->json(["status" => $status,"message"=>$res_message]);
}else{
return response()->json(["status" => 'ERROR',"message"=>'Invalid request format']);
}
}
`
但是当我用token = ALL发送post请求时,我得到错误
我认为问题在这里
if($token == "ALL"){
$token = "/topics/all";
}
如何解决这个问题?
答案 0 :(得分:0)
我认为问题在这里
这不是问题所在。这就是解决方案。
代码说"如果令牌是 ALL ,那么使用 / topics / all 而不是"。
当您手动发出请求时,您将发送 ALL 而不是 / topics / all 。