我正在尝试使用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;
}
尝试显式转换 $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
是
注册令牌,通知密钥或主题。
请参阅以下内容:Firebase Cloud Messaging HTTP协议
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句