【发布时间】:2014-10-13 21:13:50
【问题描述】:
您好,我正在尝试将 GCM 用于我的 Android 应用程序。
代码在我的服务器中非常好。当我移动到 godady 服务器中的客户端服务器时。它不起作用我检查了服务器和浏览器密钥,但没有运气,我收到 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>
【问题讨论】:
-
您的代码看起来不错,与我使用的和我的作品非常相似。根据 GCM 参考,这是一个身份验证错误并且有一个故障排除,请参阅:developer.android.com/google/gcm/http.html#auth_error
标签: android google-cloud-messaging