【发布时间】: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