0

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?

halfer
  • 18,701
  • 13
  • 79
  • 158
Wai Yan Hein
  • 9,841
  • 21
  • 103
  • 244

1 Answers1

0

The error you receive is 401 HTTP status code, which means that the sender account used to send a message couldn't be authenticated. Here are the possible causes:

  • Authorization header missing or with invalid syntax in HTTP request.

  • Invalid project number sent as key.

  • Key valid but with GCM service disabled.

  • Request originated from a server not whitelisted in the Server Key IPs.

  • The API key is not valid.

Check that the token you're sending inside the Authentication header is the correct API key associated with your project. See Checking the validity of an API Key for details.

Also try to check this related SO question 17969191 and 11242743 for more information.

Community
  • 1
  • 1
KENdi
  • 7,205
  • 2
  • 14
  • 26