【问题标题】:Error in sending request Google Cloud Messaging in PHP在 PHP 中发送请求 Google Cloud Messaging 时出错
【发布时间】:2016-04-25 16:28:57
【问题描述】:

我正在尝试使用 Google Clouding Messaging API 与 Android 应用程序集成。对于后端,我使用的是 Laravel 5.2。我在 Google API 中生成了 3 个 api 密钥。这些是服务器 API 密钥、Android API 密钥和浏览器 API 密钥。我引用了这个tutorial

这是我从服务器到 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;
}

我调用了注册函数。 emailnamereg_ids 是模拟值。我刚刚分别通过了myemailmyname 和随机字符串。注册功能是控制器的动作。对于GOOGLE_API_KEY,我传递了服务器 api 密钥。但是当我请求时,它给了我以下错误。

{"multicast_id":5065519232839143946,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

我的推送请求正确吗?我可以将任何唯一值传递给reg_ids。另外,我的GOOGLE_API_KEY应该是Android、服务器还是浏览器的key?

【问题讨论】:

    标签: php android google-cloud-messaging


    【解决方案1】:

    您收到的错误是401 HTTP status code,这意味着用于发送邮件的发件人帐户无法通过身份验证。以下是可能的原因:

    • HTTP 请求中缺少授权标头或语法无效。

    • 作为密钥发送的项目编号无效。

    • 密钥有效,但 GCM 服务已禁用。

    • 请求来自未在服务器密钥 IP 中列入白名单的服务器。

    • API 密钥无效。

    检查您在 Authentication 标头中发送的令牌是否是与您的项目关联的正确 API 密钥。详情请见Checking the validity of an API Key

    还可以尝试检查这个相关的 SO 问题 1796919111242743 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 2018-11-02
      • 2014-02-21
      • 2021-11-22
      • 2019-09-29
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多