2013-07-31 1 views
0

PHP 런타임을 사용하여 appengine에서 gcm을 사용하려고합니다. 다음 URL 가져 오기 서비스PHP GCM appengine

$context = array("https"=> 
      array("method" => "post", 
       "content" => json_encode($fields), 
       "header" => "Content-Type: application/json\r\n" . 
          "Authorization: key=" . GOOGLE_API_KEY . "\r\n" 
      ) 
     ); 
    $context = stream_context_create($context); 
    $result = file_get_contents($url, false, $context); 

를 사용하는 코드는 PHP 컬을 사용하는 기존 코드는 다음과 같은 :

$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); 
에서 appengine의 URL을 가져올 서비스를 사용

PHP 컬이 잘 작동 사용하는 코드,하지만 코드 일하지 않아. 누군가 내가 잘못하고 있다고 나에게 말할 수 있습니까?

+0

어떤 오류가 나타 납니까? –

+0

오류가 표시되지 않습니다. 하지만 'https://android.googleapis.com/gcm/send'가 'http://developer.android.com/google/gcm/index.html'으로 리디렉션되고 있으며 GCM 메시지가 전송되지 않습니다. –

+0

주세요 내 솔루션에 봐. 그것은 일하고 : http://stackoverflow.com/questions/17998875/googlecloudmessaging-returning-invalidregistration –

답변

4

테스트 된 코드입니다.

 
    public function sendAndroidPushNotification($registration_ids, $message) 
    { 
     // Adding devicetoken in array 
     $registrationIds = array($registration_ids); 
     $msg = array(
      'message' => $message, 
      'title' => 'notification center', 
      'tickerText' => $message, 
      'vibrate' => 1, 
      'sound' => 1, 
     ); 

     $fields = array(
      'registration_ids' => $registrationIds, 
      'data' => $msg 
     ); 
     $fields = json_encode($fields); 
     $arrContextOptions=array(
      "http" => array(
       "method" => "POST", 
       "header" => 
        'Authorization: key = '. "\r\n" . 
        'Content-Type: application/json'. "\r\n", 
       "content" => $fields, 
      ), 
      "ssl"=>array(
       "allow_self_signed"=>true, 
       "verify_peer"=>false, 
      ), 
     ); 
     $arrContextOptions = stream_context_create($arrContextOptions); 
     $result = file_get_contents('https://android.googleapis.com/gcm/send', false, $arrContextOptions); 

     return $result; 
    } 
+0

찾고 있었어, 고마워요! 코드를 더 형식화 할 수 있니? – Sam