在php中管理推送通知的响应

时间:2018-02-12 12:08:46

标签: php firebase push-notification firebase-cloud-messaging

这是我的通知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"}

1 个答案:

答案 0 :(得分:1)

如果只想显示

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

然后写

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

在您的代码中。

它可以解决您的问题