我正在尝试使用谷歌云消息中的推送通知,我正在传递一个数组om注册ID,但由于某种原因,我得到以下错误:
<?php
function getdb() {
$db = pg_connect("...") or die('connection failed');
return $db;
}
?>
这是我试图传递的部分:
Field "data" must be a JSON array: You have just invited to play 'Soccer'
不是gcm的代码是:
"registred_ids":["APA91bF9itasGCSK8NbD9u5GJWnEmbWCdS0sEn_xxRVbVpfI0_3FKkvxVBr5xtdE26VZgOO8rCBpf4CaAzHUbMj7PmmDxqpdbWO6VBrPB8dW4CPqPovJbnB_p1Ha_fuwyf8SnOqgOFajK8HEdiZO65qUljO2wCuuDQ"]
和传递注册ID的代码是:
<?php
class GCM {
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once 'connection.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' .GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
}
?>
答案 0 :(得分:1)
使用此 -
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => array("message" => $message),
);
答案 1 :(得分:-1)
它说数据字段需要是一个数组,但你要将它设置为$ message,这是一个字符串,而不是一个数组。
答案 2 :(得分:-1)
Field "data" must be a JSON array: You have just invited to play 'Soccer'
这意味着数据节点中传递的值不是json。 将$ message编码到json中将修复错误。