我正在尝试使用以下功能将推送通知发送到移动设备,但无法发送。我正在通过Firebase检查,它可以正常工作,但无法通过我的编码发送。
这是我的功能,并给出如下结果, 结果=
“ {” multicast_id“:7523757847702384710,”成功“:1,”失败“:0,” canonical_ids“:0,”结果“:[{” message_id“:” 0:1558857210405129%6298e0406298e040“}]}}”
public function sendNotification(Request $req)
{
$fcm_token=$req->input('fcm_token');
$json_data=[
"to" => $fcm_token,
"notification" => [
"body" => "hello",
"title" => "hello"
]
];
$data = json_encode($json_data);
//FCM API end-point
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'AIzaSyDF4_rqij_Bt4zC3GNEVbNFvwX1HHxy5Mo';
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
//CURL request to route notification to FCM connection server (provided by Google)
$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, $data);
$result = curl_exec($ch);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
dd($result);
}
我发送通知时,移动设备会收到推送通知。