4

설정 Google 개발자 콘솔 Google Cloud Messaging for Android.GCM 서버 측 PHP - 무단 401 오류

신임장에서는 참조에 0.0.0.0을 입력하는 브라우저 API 키를 작성합니다. 사실 나는 다른 튜토리얼에서 다른 표시를 발견했기 때문에 두 종류의 키를 생성합니다. 두 키 (

server-key picture

browser-key picture

나는

<? 
/** 
* The following function will send a GCM notification using curl. 
* 
* @param $apiKey  [string] The Browser API key string for your GCM account 
* @param $registrationIdsArray [array] An array of registration ids to send this notification to 
* @param $messageData  [array] An named array of data to send as the notification payload 
*/ 
function sendNotification($apiKey, $registrationIdsArray, $messageData) 
{ 
    $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey); 
    $data = array(
     'data' => $messageData, 
     'registration_ids' => $registrationIdsArray 
    ); 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send"); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 

    $response = curl_exec($ch); 
    curl_close($ch); 

    return $response; 
} 
?> 
<? 
// Message to send 
$message  = "the test message"; 
$tickerText = "ticker text message"; 
$contentTitle = "content title"; 
$contentText = "content body"; 

$registrationId = '372CBFD0C4BFE728'; 
$apiKey = "AIzaSyDeNN1XJBFGE_lJ_35VMUmx5cUbRCUGkjo"; 

$response = sendNotification( 
       $apiKey, 
       array($registrationId), 
       array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText)); 

echo $response;  
?> 

내가 그

{"multicast_id":6782339717028231855,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]} 

뭔가를 얻을 것으로 예상이 PHP 스크립트를 사용하여 키를 테스트하지만 취득) Unauthori zed 401 오류.

도움 주셔서 감사합니다.

+0

나를 가리켜 주셔서 감사합니다. curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, 0); 이 권한이 없으면 401 오류 – HiB

답변

7

허용 된 참조 또는 허용 된 IP 주소로 0.0.0.0을 입력하는 대신 아무 것도 입력하지 마십시오. 그게 효과가있다.

+0

이 작동합니다! 정말 고맙습니다. – user3631494

+0

나는 또한 동일한 오류가 발생합니다. 어떻게 해결 했습니까? 저는 localhost에서 테스트를하고 무단 401 오류가 발생했습니다. 여기서 도와 줄 수 있니? – Bora

0

IP 주소 필드에 아무 것도 입력하지 마십시오. 마치 매력처럼 작동합니다. 또한 서버 측에서 적절한 Server 키와 적절한 발신자 ID android 클라이언트를 사용하고 있는지 확인하십시오.