Android GCM 401未授权错误php

Android GCM 401未授权错误php,android,google-cloud-messaging,Android,Google Cloud Messaging,嗨,我正在尝试将GCM用于我的android应用程序 代码在我的服务器中非常好。当我移动到godady服务器中的客户机服务器时。它不工作,我检查了服务器和浏览器键,但没有运气,我得到401错误。在客户端服务器中是否有需要禁用的防火墙?告诉我。提前谢谢 下面是我的php代码 function sendPushNotificationToGCM($registatoin_ids, $message) { //Google cloud messaging GCM-API url $u

嗨,我正在尝试将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>
函数sendPushNotificationToGCM($registatoin\u id,$message){
//谷歌云消息GCM-API url
$url='1https://android.googleapis.com/gcm/send';
$fields=数组(
“注册ID”=>$RegistratoIn\u ID,
“数据”=>$message,
);
//谷歌云消息GCM API密钥
定义(“谷歌API密钥”、“我的API密钥”);
$headers=数组(
“Authorization:key=”。GOOGLE_API_key,
'内容类型:应用程序/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\u exec($ch);
如果($result==FALSE){
模具('Curl失败:'。Curl_错误($ch));
}
卷曲关闭($ch);
返回$result;
}
PHP中的Google云消息传递(GCM)服务器
PHP中的Google云消息传递(GCM)服务器


确保以下键正确:

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

如果您使用的是浏览器密钥,您还需要确保自动调整该密钥的站点ip地址。

在我的情况下,开发人员控制台上设置的ip与我的服务器ip不匹配-除了@meda的回答中提到的API_密钥检查之外,您还应该重新检查


请注意,出于测试目的,IP可能被设置为0.0.0.0/0,这在更换我的服务器IP后对我很有效&我的服务器也是godaddy提供的。

您的代码看起来不错,与我使用的代码非常相似,我的工作也很正常。根据GCM参考,这是一个身份验证错误,存在故障排除,请参阅以下内容: