Php 谷歌FCM字段“;“数据”;必须是JSON数组
我正在尝试在Android设备上发送Google FireBase推送通知。遵循我的代码Php 谷歌FCM字段“;“数据”;必须是JSON数组,php,android,json,codeigniter,push-notification,Php,Android,Json,Codeigniter,Push Notification,我正在尝试在Android设备上发送Google FireBase推送通知。遵循我的代码 function send_android_notification($deviceToken="",$message="",$input_data=array()) { $apiKey = 'XXXXXXXXXX'; // Set POST variables $url = 'https://fcm.googleapis.com/fcm/send'; $
function send_android_notification($deviceToken="",$message="",$input_data=array())
{
$apiKey = 'XXXXXXXXXX';
// Set POST variables
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
//'to' => $deviceToken,
'registration_ids' => array($deviceToken),
'notification'=> array( "body" => $message,"title"=>"ABCD","icon"=>""),
'data' => $input_data
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init($url);
// Set the url, number of POST vars, POST data
//curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result1 = curl_exec($ch);
echo $result1;
curl_close($ch);
}
但当我发送推送通知时,它会给我以下错误
字段“数据”必须是JSON数组:[]您需要将数组编码为JSON
json\u encode($input\u data)
您需要将数组编码为json
json\u encode($input\u data)
试试这个json\u encode($fields,json\u FORCE\u OBJECT)谢谢@Scaffold你节省了我的时间。完美无缺地尝试这个json_编码($fields,json_FORCE_OBJECT)谢谢@Scaffold你节省了我的时间。完美无缺