2013-04-25 2 views
0

저는 GCM을 처음 사용하고 Android 프로그래밍에 익숙하지 않습니다. 푸시 알림을 구현하려고합니다. 내 MySQL 데이터베이스에 gcm_reg_no가 있습니다.GCM 구현에 의문의 여지가 있음

이제 푸시 알림을이 등록 ID로 보내려고합니다. 다음 단계가 무엇인지 알게 도와 주시겠습니까? 프로젝트의 Android 부분에 아무 것도 추가해야합니까? 또는 PHP를 사용하여 GCM 서버에 요청을 보내면됩니까?

+0

시작하기 가장 좋은 장소는 공식적인 예입니다. 서버 부분과 클라이언트 부분을 설명합니다. 그래서 네, 당신은 당신의 안드로이드 프로젝트에서 뭔가를해야합니다. [예제] (http://developer.android.com/google/gcm/demo.html) – wtsang02

+0

애플리케이션의 서버 부분에 대한 책임은 무엇입니까? –

답변

0

서버에 다시 게시되어 DB에 저장된 기기의 GCM 등록 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; 
} 

당신이 CURL이 PHP에서 활성화되어 있는지 확인하는 것을 잊지 마세요 이 예제와 함께 거기에 가이드의 할당한다있다 및 GCM, 행복한 푸시 메 시징 시작하기 : D

관련 문제