您好,我正在尝试在我的 Android 应用程序上使用 GCM。
我的问题是,当尝试发送消息时,我收到 401 错误,未经授权的错误。
我在谷歌云控制台中为服务器应用程序创建了 key ,并按照以下说明进行操作:http://developer.android.com/google/gcm/gs.html
这是我的代码:
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;
}
}
从我的应用程序中,我将 POST 发送到此 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 都被允许并且可以正常工作。
关于php - GCM 服务器出现 401 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766403/