-1

I'm using GCM in php. Everything is fine. But im getting response as

"Field \"data\" must be a JSON array: example\n"

My code for GCM is

function sendNotification($registrationIdsArray, $messageData) {
        $data = array(
            'data' => $messageData,
            'registration_ids' => $registrationIdsArray
        );
        var_dump($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
        curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }

and my $data object is

{"data":"example","registration_ids":["apikey1", "apikey2"]}

What is the missing code for GCM here.

Deen
  • 611
  • 2
  • 16
  • 30

1 Answers1

0

the data which you are passing should be an array not a string like this

  $messageData =  array(
    'success' => 1,
    'title' => $data['title'],
    'desc' => $data['message']
  );
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40