Php 401来自GCM服务器的错误
你好,我正在尝试在我的android应用程序上使用GCM 我的问题是,当试图发送消息时,我得到401错误,未经授权的错误 我在google云控制台中为服务器应用程序制作了一个密钥,并按照以下说明操作: 这是我的代码:Php 401来自GCM服务器的错误,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,你好,我正在尝试在我的android应用程序上使用GCM 我的问题是,当试图发送消息时,我得到401错误,未经授权的错误 我在google云控制台中为服务器应用程序制作了一个密钥,并按照以下说明操作: 这是我的代码: class GCM { //put your code here // constructor function __construct() { } /** * Send
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Content-Type: application/json',
'Authorization: key=**MY_KEY***'
);
// 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;
}
}
从我的应用程序中,我向以下PHP代码发送了帖子:
include_once './GCM.php';
$gcm = new GCM();
$message = array("status" => "1");
$ids = array('**my_gcm_id**');
$result = $gcm->send_notification($ids, $message);
echo $result;
我删除了所有允许的ip,以便允许任何ip及其工作。我删除了所有允许的ip,以便允许任何ip及其工作。确保您的API密钥使用的是GCM“服务器密钥”而不是“安卓密钥”。确保您使用的是GCM“服务器密钥”而不是“安卓密钥”您的API密钥。我的服务器出现了类似的问题,删除IP也对我有效。奇怪的是,我可以通过nodejs发送,但不能从我的.netweb应用程序发送。我在这上面浪费了几个小时,所以谢谢你的建议。我的服务器也有类似的问题,删除IP也对我有帮助。奇怪的是,我可以通过nodejs发送,但不能从我的.netweb应用程序发送。我在这上面浪费了好几个小时,所以谢谢你的建议。