gpt4 book ai didi

php - 字段 "to"必须是 JSON 字符串 [Firebase]

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:32 25 4
gpt4 key购买 nike

我正在尝试使用 cron 作业发送通知。我从 GCM 迁移到 FCM。在我的服务器端,我将 https://android.googleapis.com/gcm/send 更改为 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;
}

最佳答案

尝试将 $registrationIDs 显式转换为 string

$message = array(
'to' => (string)$registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);

<罢工>

编辑后的答案

'to' 参数需要一个string - 它是消息的接收者。

$registrationIDs 可以通过一个单独的参数(作为字符串数组)传递给 'registration_ids'

将您的代码编辑成如下内容:

$recipient = "YOUR_MESSAGE_RECIPIENT";

$message = array(
'to' => $recipient,
'registration_ids' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);

$recipient 在哪里

a registration token, notification key, or topic.

引用这个:Firebase Cloud Messaging HTTP Protocol

关于php - 字段 "to"必须是 JSON 字符串 [Firebase],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com