我正在尝试使用 cron 作业发送通知。我从 GCM 迁移到 FCM。在我的服务器端,我更改https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send
并更新了如何请求将数据更改registration_ids
为to
. 检查传递的 json 是一个有效的 json,但我有一个错误Field "to" must be a JSON string
。有没有办法解决这个问题?
这是我的代码
function sendNotificationFCM($apiKey, $registrationIDs, $messageText,$id) {
$headers = array(
'Content-Type:application/json',
'Authorization:key=' . $apiKey
);
$message = array(
'to' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($message)
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}