【发布时间】:2018-01-10 19:19:14
【问题描述】:
我有 fcm 服务器。在 Laravel lumen 中构建。
服务器地址: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";
}
如何解决?
【问题讨论】:
-
你用过 JWT 吗?如果您使用 JWT,那么您必须使用其他变量而不是使用令牌。用于验证用户的令牌变量。
-
没有。我不使用任何 jwt
-
你添加了发布路线吗??
-
yes.post 与用户令牌正常工作。看图片
-
$fields = ['to' => $token,'data'=> array('title' => $title, 'body' => $body)];问题就在这里。你不能发送给所有你必须使用所有令牌为每个用户发送通知。
标签: php json firebase firebase-cloud-messaging lumen