使用laravel的推送通知无法在手机上获得。

问题描述 投票:0回答:1

我想使用以下功能向移动设备发送推送通知,但无法发送。我正在通过firebase检查,它可以工作,但通过我的编码无法发送。

这是我的函数& 给出的结果如下,结果是

"{"multicast_id":7523757847702384710, "success":1, "fail":0, "canonical_ids":0, "results":[{"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);

}

当我发送通知时,手机收到推送通知。

firebase laravel-5.7
1个回答
1
投票

在laravel中,我们通常使用 笤帚 包,通过laravel发送推送通知,而不是使用核心php。

https:/packagist.orgpackagesbrozotlaravel-fcm.

© www.soinside.com 2019 - 2024. All rights reserved.