Google FCM Field"数据"必须是JSON数组

时间:2017-04-10 07:46:30

标签: 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';
    $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数组:[]

1 个答案:

答案 0 :(得分:0)

您需要将数组编码为json

json_encode($input_data)