PHP 的 GCM 警告
Warning on GCM with PHP
我在 php 中使用 GCM。一切都很好。但我得到的回应是
"Field \"data\" must be a JSON array: example\n"
我的 GCM 代码是
function sendNotification($registrationIdsArray, $messageData) {
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
我的 $data 对象是
{"data":"example","registration_ids":["apikey1", "apikey2"]}
此处缺少的 GCM 代码是什么。
您传递的数据应该是数组而不是像这样的字符串
$messageData = array(
'success' => 1,
'title' => $data['title'],
'desc' => $data['message']
);
我在 php 中使用 GCM。一切都很好。但我得到的回应是
"Field \"data\" must be a JSON array: example\n"
我的 GCM 代码是
function sendNotification($registrationIdsArray, $messageData) {
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
我的 $data 对象是
{"data":"example","registration_ids":["apikey1", "apikey2"]}
此处缺少的 GCM 代码是什么。
您传递的数据应该是数组而不是像这样的字符串
$messageData = array(
'success' => 1,
'title' => $data['title'],
'desc' => $data['message']
);