android - GCM 401 未经授权的错误 php

标签 android google-cloud-messaging

您好,我正在尝试为我的 Android 应用程序使用 GCM。

我的服务器中的代码完全没问题。当我移动到 godady 服务器中的客户端服务器时。它不工作我检查了服务器和浏览器 key 但没有运气我收到 401 错误。有没有我需要在客户端服务器中禁用的防火墙告诉我。提前致谢

下面是我的php代码

function sendPushNotificationToGCM($registatoin_ids, $message) {
    //Google cloud messaging GCM-API url
     $url        = 'https://android.googleapis.com/gcm/send';
    $fields     = array(
    'registration_ids' => $registatoin_ids,
    'data' => $message,
);
// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "my-api-key");
$headers = array(
    'Authorization: key=' . GOOGLE_API_KEY,
    'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result == FALSE) {
    die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}

<html>
<head>
<title>Google Cloud Messaging (GCM) Server in PHP</title>
</head>
<body>
<h1>Google Cloud Messaging (GCM) Server in PHP</h1>
 <form method="post" action="index.php/?push=1">
 <div>
    <textarea rows="2" name="message" cols="23" placeholder="Message to transmit via GCM"></textarea>
</div>
<div><input type="submit"  value="Send Push Notification via GCM" /></div>
</form>
 <p><h3><?php echo $pushStatus; ?></h3></p>
  </body>
  </html>

最佳答案

确保以下 key 正确:

// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "my-api-key");

如果您使用的是浏览器 key ,您还需要确保为该 key 自动设置您的站点 IP 地址。

关于android - GCM 401 未经授权的错误 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23422214/

相关文章:

java - MobileAds.initialize(Context, String) 已弃用

java - Libgdx 在生成应用程序时卡住了

android - PushSharp 是否需要打开运行它的机器的入站端口?

android - 当用户使用不同的智能手机时,如何在 GCM/FCM 中交换 Registration_ID?

google-cloud-messaging - Google PubSub 错误 [代码=8a75]

google-cloud-messaging - 某些用户未触发 GcmTaskService

安卓 ||获取当前谷歌地图路线信息

android - Google Play 服务版本 5.2.08 对我的设备来说太新了

android - "CleverTap"Can 't see "Event Property"filter in Filter by event property in some events

android - Knox 内部的 Google 云消息传递。如何接收消息?