如何将FCM推送通知结果存储到变量中?

时间:2018-08-01 14:01:45

标签: php curl push-notification

这是我的代码:

<?php
$url = "https://fcm.googleapis.com/fcm/send";
$token = "*****";
$serverKey = "*****"; 

$title = "This is Title";
$body = "Body of the message";
$param_arr = array();
$param_arr['page_name'] = "notification";
$param_arr['id'] = "10";

$notification = array('title' =>$title , 'is_background' =>'' , 'text' => $body, 'image' => '', 'payload' => $param_arr);

$arrayToSend = array('to' => $token, 'notification' => $notification, 'data' => $notification,'priority'=>'high', 'timestamp'=>date("Y-m-d H:i:s"));
$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_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_ENCODING,'');
$response = curl_exec($ch);
curl_close($ch);

?>

并在浏览器中显示结果:

{"multicast_id":5813147991035715680,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

现在我需要从结果中获取成功或失败的状态, 如何获取状态并将其存储到变量?

$ response提供布尔响应

var_dump($response);

1 个答案:

答案 0 :(得分:0)

默认情况下,curl_exec将响应字符串发送到标准输出(在您使用的浏览器中)。

要将结果存储在变量中,应将CURLOPT_RETURNTRANSFER设置为true。

我认为这会起作用。

请不要告诉我您也尝试过并发现否定。 :-)