2013-04-25 92 views
0

我是GCM的新手,也是Android編程方面的新手。我正在嘗試實施推送通知。我在我的MySQL數據庫中有gcm_reg_no。GCM執行疑惑

現在我想將推送通知發送到這些註冊ID。你能幫我知道下一步會是什麼嗎?我需要在項目的Android部分添加任何內容嗎?或者我只需要將請求發送到使用PHP的GCM服務器?

+0

最好的地方是官方的例子。 Theres服務器部分和客戶端部分。所以是的,你需要在你的Android項目中做些什麼。 [示例](http://developer.android.com/google/gcm/demo.html) – wtsang02 2013-04-25 00:18:46

+0

您能否詳細解釋應用程序的哪些服務器部分將負責? – 2013-04-25 00:28:37

回答

0

如果從回傳到你的服務器和存儲在您的數據庫設備的GCM REG的ID,那麼所有,當你想要做一個推送通知給他們,你所要做的就是將數據傳遞到GCM服務器。

例如,下面是我用來發送推送通知功能:

//reg array here is an array of the GCM_reg_Ids I wish to send the push to 
function push($regarr) 
{ 

// Set POST variables 
$url = 'https://android.googleapis.com/gcm/send'; 

// Google API KEY, If you don't know your key just walk through the starter guide 
$apiKey = "***************"; 

// Get all the message variables 
// These are variables of data you send in the push notification which you use in the 
// Appcode to do stuff 
$message = "test" ; 
$data = "http://www.google.com"; 
$preheader = "New Notification"; 
$header = "test" ; 

$fields = array(
       'registration_ids' => $regarr, 
       'data'    => array("message" => $message, 
              "preheader" => $preheader, 
              "header" => $header, 
              "data" => $data), 
      ); 

    $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)); 

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

    // Close connection 
    curl_close($ch); 


    $json_return = json_decode($result); 
    return $json_return; 
} 

不要忘記,以確保你有你的支持PHP的捲曲,有配發導遊那裏有例子和開始使用GCM,快樂推送消息:D