gpt4 book ai didi

php - 来自 GCM 服务器的 401 错误

转载 作者:行者123 更新时间:2023-11-29 16:03:28 25 4
gpt4 key购买 nike

<分区>

您好,我正在尝试在我的 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;
}

}

我从我的应用程序向此 PHP 代码发送了 POST:

    include_once './GCM.php';

$gcm = new GCM();

$message = array("status" => "1");

$ids = array('**my_gcm_id**');

$result = $gcm->send_notification($ids, $message);

echo $result;

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com