I am trying to use Google Clouding Messaging API to integrate with Android App. For backend, I am using Laravel 5.2. I generated 3 api keys in Google API. These are server API key, Android API key and browser API key. I am referencing on this tutorial.
This is my push request from server to GCM server:
private function sendNotification($registatoin_ids,$message)
{
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key='.GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
function register(Request $request)
{
$name = $request->name;
$email = $request->email;
$gcm_regid = $request->reg_ids;
$registatoin_ids = array($gcm_regid);
$message = array("product" => "shirt");
$result = $this->sendNotification($registatoin_ids,$message);
echo $result;
}
I called register function. email
, name
and reg_ids
are mock values. I just passed myemail
, myname
and random string respectively. Register function is the action of the controller. For GOOGLE_API_KEY
, I passed server api key. But when I request, it is giving me following error.
{"multicast_id":5065519232839143946,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Is my push request correct? Can I pass any unique value to reg_ids
. Moreover, is my GOOGLE_API_KEY
should be Android, server or browser key?