谷歌GCM服务器返回未经授权的错误401
2022-08-30 14:47:26
我正在使用GCM服务从服务器推送信息。如果我使用浏览器键,它将成功评估显示为:{“multicast_id”:4849013215736515938,“成功”:1,“失败”:0,“canonical_ids”:0,“结果”:[{“message_id”:“0:1348742583011905%2adac3a0f9fd7ecd”}]}},但我没有在设备上收到任何通知。如果我使用服务器密钥,它会显示未经授权的错误401。我的代码如下所示:
$apiKey = "xxxxx";
$registrationIDs = array("xxxxxxxx");
$message = "testing Process";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array("message"=>$message),
);
$headers = array(
'Authorization: key=' . $apiKey,
'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, 0);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );
$result = curl_exec($ch);
if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); }
curl_close($ch);
echo $result;
请帮我解决这个问题。提前致谢。