Field "to" must be a JSON string [Firebase]

11,908

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"
    ]
];
Share:
11,908
natsumiyu
Author by

natsumiyu

Updated on June 08, 2022

Comments

  • natsumiyu
    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 changing registration_ids to to. Checking the json passed it is a valid json but I'm having an error Field "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
    natsumiyu almost 8 years
    will (string) convert the $registrationIDs like "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvD, 1234asd:CI2k_HHwgIpoDKCIZvvD, bk3RNwTe3H0:asds_HwgIpoDKCIZvvD" this?
  • jayeshsolanki93
    jayeshsolanki93 almost 8 years
    No, 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
    natsumiyu almost 8 years
    my $registrationIDs have the registration tokens
  • jayeshsolanki93
    jayeshsolanki93 almost 8 years
    @mori then 'to' is optional, see the link in the answer
  • natsumiyu
    natsumiyu almost 8 years
    should I only use registration_ids ? but when i use that my device doesn't receive any notification.
  • jayeshsolanki93
    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
    Ali_Ai_Dev over 5 years
    Must use either "registration_ids" field or "to", not both