Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌FCM字段“;“数据”;必须是JSON数组_Php_Android_Json_Codeigniter_Push Notification - Fatal编程技术网

Php 谷歌FCM字段“;“数据”;必须是JSON数组

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'; $

我正在尝试在Android设备上发送Google FireBase推送通知。遵循我的代码

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你节省了我的时间。完美无缺