【问题标题】:Google FCM Field "data" must be a JSON arrayGoogle FCM 字段“数据”必须是 JSON 数组
【发布时间】:2017-09-05 03:53:17
【问题描述】:

我正在尝试在 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_encode($fields , JSON_FORCE_OBJECT)
  • 谢谢@Scaffold 你节省了我的时间。完美完成

标签: php android json codeigniter push-notification


【解决方案1】:

您需要将数组编码为 json

json_encode($input_data)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多