简体   繁体   English

在PHP中发送请求Google Cloud Messaging时出错

[英]Error in sending request Google Cloud Messaging in PHP

I am trying to use Google Clouding Messaging API to integrate with Android App. 我正在尝试使用Google Clouding Messaging API与Android App集成。 For backend, I am using Laravel 5.2. 对于后端,我正在使用Laravel 5.2。 I generated 3 api keys in Google API. 我在Google API中生成了3个api密钥。 These are server API key, Android API key and browser API key. 这些是服务器API密钥,Android API密钥和浏览器API密钥。 I am referencing on this tutorial . 我正在参考本教程

This is my push request from server to GCM server: 这是我从服务器到GCM服务器的推送请求:

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. emailnamereg_ids是模拟值。 I just passed myemail , myname and random string respectively. 我只是分别传递了myemailmyname和random string。 Register function is the action of the controller. 寄存器功能是控制器的动作。 For GOOGLE_API_KEY , I passed server api key. 对于GOOGLE_API_KEY ,我传递了服务器api密钥。 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 . 我可以将任何唯一值传递给reg_ids Moreover, is my GOOGLE_API_KEY should be Android, server or browser key? 此外,我的GOOGLE_API_KEY应该是Android,服务器还是浏览器密钥?

The error you receive is 401 HTTP status code , which means that the sender account used to send a message couldn't be authenticated. 您收到的错误是401 HTTP状态代码 ,这意味着用于发送消息的发件人帐户无法通过身份验证。 Here are the possible causes: 以下是可能的原因:

  • Authorization header missing or with invalid syntax in HTTP request. 授权标头丢失或HTTP请求中的语法无效。

  • Invalid project number sent as key. 无效的项目编号作为密钥发送。

  • Key valid but with GCM service disabled. 密钥有效,但禁用了GCM服务。

  • Request originated from a server not whitelisted in the Server Key IPs. 请求源自未在服务器密钥IP中列入白名单的服务器。

  • The API key is not valid. API密钥无效。

Check that the token you're sending inside the Authentication header is the correct API key associated with your project. 检查您在Authentication标头中发送的令牌是否是与您的项目关联的正确API密钥。 See Checking the validity of an API Key for details. 有关详细信息,请参见检查API密钥的有效性

Also try to check this related SO question 17969191 and 11242743 for more information. 另请尝试检查此相关的SO问题1796919111242743,以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM