管理PHP中推送通知的响应



这是我的通知comman函数

   public function notification($token,$title,$body){
           $url = "https://fcm.googleapis.com/fcm/send";
            //$token = "clyEl";
            $serverKey = 'AAAAG';
            //$title = "Title";
            //$body = "Body of the message";
            $notification = array('title' =>$title , 'text' => $body, 'sound' => 'enable', 'badge' => '1');
            $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
            $json = json_encode($arrayToSend);
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'Authorization: key='. $serverKey;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
            //Send the request
            $result=curl_exec($ch);
    }

**此代码调用其他功能**

 $send_notification=$this->notification($token,$title,$body);
    return response()->json(['status'=>true,'message'=>'request send successfully']);

,但是在JSON响应中,我得到这样的响应

{"multicast_id":6583845632900792550,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1518436953401414"}]}{"status":true,"message":"request send successfully"}

我只需要这样的

{"status":true,"message":"request send successfully"}

如果您只想显示

{"status":true,"message":"request send successfully"}

然后写

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

在您的代码中。

它解决了您的问题

最新更新