Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sending data with push notification to Android with PHP

According to this docs, there is the field data which I’m trying to use:

$fields = [
    'message' => [
        'token' => $deviceToken,
        'notification' => [
            'title' => $notifTitle,
            'body' => $notifDesc
        ],
        'data' => $data
    ]
]; 

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/v1/projects/bla/messages:send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );

$result = curl_exec($ch) . '<br><br>';
print($result);

This is what $data contains:

array(11) {
  ["title"]=>
  string(21) "Antwort auf Kommentar"
  ["msg"]=>
  string(29) "Cornholio hat dir geantwortet"
  ["image"]=>
  string(12) "30321555.jpg"
  ["notifType"]=>
  string(5) "reply"
  ["channelID"]=>
  string(21) "channel_reply_comment"
  ["memeID"]=>
  int(20202)
  ["memeTitle"]=>
  string(10) "meme title"
  ["meme"]=>
  string(19) "TrlNO38.mp4"
  ["size"]=>
  string(7) "460|818"
  ["commentCount"]=>
  int(7)
  ["mentioned"]=>
  int(1)
}

And I get this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{ "error": { "code": 400, "message": "Invalid value at ‘message.data[5].value’ (TYPE_STRING), 20202\nInvalid value at ‘message.data[9].value’ (TYPE_STRING), 7\nInvalid value at ‘message.data[10].value’ (TYPE_STRING), 1", "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ { "field": "message.data[5].value", "description": "Invalid value at ‘message.data[5].value’ (TYPE_STRING), 20202" }, { "field": "message.data[9].value", "description": "Invalid value at ‘message.data[9].value’ (TYPE_STRING), 7" }, { "field": "message.data[10].value", "description": "Invalid value at ‘message.data[10].value’ (TYPE_STRING), 1" } ] } ] } }

I was able to send data like this with the old API, but how to do this with the current one?

>Solution :

The error you’re encountering is because the FCM (Firebase Cloud Messaging) API expects all the values in the data field to be strings. However, in your $data array, some values are integers (e.g., memeID, commentCount, and mentioned). The error message indicates that these integer values are not being automatically converted to strings, which is required by the FCM API.

To resolve this, you need to explicitly cast all non-string values to strings before including them in the data array. Here’s how you can do it:

$data = [
"title" => "Antwort auf Kommentar",
"msg" => "Cornholio hat dir geantwortet",
"image" => "30321555.jpg",
"notifType" => "reply",
"channelID" => "channel_reply_comment",
"memeID" => strval(20202),  // Cast to string
"memeTitle" => "meme title",
"meme" => "TrlNO38.mp4",
"size" => "460|818",
"commentCount" => strval(7),  // Cast to string
"mentioned" => strval(1)  // Cast to string
];

$fields = [
'message' => [
    'token' => $deviceToken,
    'notification' => [
        'title' => $notifTitle,
        'body' => $notifDesc
    ],
    'data' => $data
    ]
  ];

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 
  'https://fcm.googleapis.com/v1/projects/bla/messages:send');
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

   $result = curl_exec($ch) . '<br><br>';
   print($result);

By using strval() to cast the integer values to strings, you ensure that the data field adheres to the FCM API’s expectations, which should resolve the INVALID_ARGUMENT error.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading