2013-07-31 32 views
0

我想在使用PHP運行時的appengine中使用gcm。以下是代碼,它使用的URLFetch服務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服務

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