2014-02-13 2 views
1

안녕하세요, 안드로이드 앱에서 GCM을 사용하려고합니다.401 GCM 서버의 오류

내 문제는 메시지를 보낼 때 401 오류, 무단 된 오류가 발생한다는 것입니다.

나는 구글 클라우드 콘솔에서 서버 응용 프로그램에 대한 키를 만들어이 지침을 따랐 : http://developer.android.com/google/gcm/gs.html

이 내 코드 :이 PHP 코드에 POST를 보내 내 응용 프로그램에서

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; 
     } 

    } 

:

include_once './GCM.php'; 

     $gcm = new GCM(); 

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

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

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

    echo $result; 

답변

4

허용 된 모든 ip가 제거되어 모든 ip가 허용되고 작동합니다.

+0

나는 내 서버에서 비슷한 문제가 있었고 IP를 삭제해도 나를 위해 일했습니다. 이상하게도, 나는 .Net 웹 어플리케이션이 아니라 nodejs를 통해 보낼 수있었습니다. 나는 이것에 시간을 낭비했다. 그래서 제안에 대해 감사해라. – raider33

0

API 키에 'Android Key'가 아닌 '서버 키'인 GCM을 사용하고 있는지 확인하십시오.