2012-09-30 68 views
-1

誰能告訴我怎樣才能獲得 「$ registrationIDs =陣列( 」REG ID1「,」 REG ID2 「);」,在下面的代碼中提到,這是張貼在這裏GCM with PHP (Google Cloud Messaging)如何從使用php的客戶端獲取註冊ID?

<?php 
      // Replace with real server API key from Google APIs 
      $apiKey = "your api key";  

       // Replace with real client registration IDs 
      $registrationIDs = array("reg id1","reg id2"); 

      // Message to be sent 
     $message = "hi Shailesh"; 

     // Set POST variables 
     $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' 
    ); 

    // 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); 
     //curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    //  curl_setopt($ch, CURLOPT_POST, true); 
     //  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

      // Execute post 
     $result = curl_exec($ch); 

     // Close connection 
      curl_close($ch); 
     echo $result; 
      //print_r($result); 
      //var_dump($result); 
     ?> 

回答

0

您需要構建一個應用程序可以用來傳遞RegID的網頁,一旦應用程序收到RegID。您必須引入某種服務器端存儲(數據庫,文件),RegID將存儲在其中。

根據您的業務,您可能想要接受一些其他參數。 POST表單對於那些人來說是個好地方。

然後在應用程序中,一旦您獲得了RegID,就會執行一個HTTP請求,同時傳遞RegID。謹慎的說法:不要在主線程中調用HttpClient。使用AsyncTask,或者旋轉新的Thread

+0

非常感謝!我已經解決了這個問題。這是我第一次在這裏寫一篇文章,也是我編碼生活的開始。 – Susheng

相關問題