Field "to" must be a JSON string [Firebase]
Solution 1
Try explicitly converting $registrationIDs
to string
.
$message = array(
'to' => (string)$registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
Edited Answer
The 'to'
parameter requires a string
- which is the recipient of the message.
The $registrationIDs
could be passed a separate parameter (as string array) to 'registration_ids'
Edit your code to something like this:
$recipient = "YOUR_MESSAGE_RECIPIENT";
$message = array(
'to' => $recipient,
'registration_ids' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
Where $recipient
is
a registration token, notification key, or topic.
Refer this: Firebase Cloud Messaging HTTP Protocol
Solution 2
Try making to as a string:
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
...
},
}
Solution 3
// if you are using an array like
$fcm_ids = array();
$fcm_ids[] = "id_1";
$fcm_ids[] = "id_2";
$fcm_ids[] = "id_3";
//.
//.
//.
$fcm_ids[] = "id_n";
// note: limit is 1000 to send notifications to multiple ids at once.
// in your messsage send notification function use ids like this
$json_data = [
"to" => implode($fcm_ids),
"notification" => [
"title" => $title,
"body" => $body,
],
"data" => [
"str_1" => "something",
"str_2" => "something",
.
.
.
"str_n" => "something"
]
];
natsumiyu
Updated on June 08, 2022Comments
-
natsumiyu almost 2 years
I'm trying to send notification using a cron job. I migrated from GCM to FCM. In my server side, I changed
https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send
and also updated on how to request the data changingregistration_ids
toto
. Checking the json passed it is a valid json but I'm having an errorField "to" must be a JSON string
. Is there anyway to solve this?Here is my code
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; }
-
natsumiyu almost 8 yearswill
(string)
convert the$registrationIDs
like "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvD, 1234asd:CI2k_HHwgIpoDKCIZvvD, bk3RNwTe3H0:asds_HwgIpoDKCIZvvD" this? -
jayeshsolanki93 almost 8 yearsNo, it wont. I guess your
$registrationIDs
is a string array. The'to'
key requires a string, and I assumed that$registrationIDs
must have been int, which it isnt, hence I suggested to cast it to string. -
natsumiyu almost 8 yearsmy
$registrationIDs
have the registration tokens -
jayeshsolanki93 almost 8 years@mori then
'to'
is optional, see the link in the answer -
natsumiyu almost 8 yearsshould I only use
registration_ids
? but when i use that my device doesn't receive any notification. -
jayeshsolanki93 almost 8 years@mori Yes, you should use
registration_ids
for sending a multicast message when you have multiple recipients. If your application is not working as intended it is unrelated to the question. Check the response code in your server code and also check for any errors in your client app(android). Post a different question regarding your problem if you cannot solve it. For now you could accept the answer if it solved your error posted in this question. -
Ali_Ai_Dev over 5 yearsMust use either "registration_ids" field or "to", not both