마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

gcm 서버부분에 대한 문의가 있습니다.

0 추천
서버에서 gcm을 보내게 되면 기본은 you got message 란 메세지를 받게 되어 있어서
중간에
 
protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
//        String message = getString(R.string.gcm_message);
        String message = "ddddddddd";
        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }
 
부분에서 메세지의 값을 임의로 dddd로 주고 실행을 시키니 값이 제대로 들어갔습니다
하지만 php 파일에서 데이터의 값을 보내주고 싶은데 어떻게 해야 할지 잘 몰라서 문의를 드리게 되었습니다.
 
 
 
 
<?php
    // 클라이언트에서 받은 Registration ID
    $regId = "장비 ID";

     // Google API Console에서 발급받은 API Key
    $apiKey = "aip 키";

    // 배열에 GCM을 수신한 Device Id를 저장 
    // 1,000개의 Registratin ID를 저장해서 한번에 Google Server에 요청 가능
    $regIds = array($regId);
  
    sendMessage($apiKey, $regIds);
  

    // Google Server에 GCM메세지를 요청
    // 요청이 성공하면 메세지는 Android OS에서 사용하는 Intent 객체에 저장되어 해당 App에 전달됨
    function sendMessage($auth, $reg) {
        $request = array(
            'registration_ids' => $reg,
            'data' => array(
                        'msg' => array(  'value1' => 'ABCDEF',
                                        'value2' => '가나다라마바사'),
                        'extra' => 'extra data'
                        )
                    );
          
        $headers = array(
            "Content-Type:application/json", 
            "Authorization:key=".$auth
        );
      
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
        $result = curl_exec($ch);
        curl_close($ch);

        // 디버깅용 메세지 출력
        echo json_encode($request);
        echo "<br />";
        echo $result;
    }  
?>

 

 
이게 php 파일부분입니다.
 
 
Noswind (2,330 포인트) 님이 2014년 8월 5일 질문

2개의 답변

0 추천
 
채택된 답변

자답

onmessage부분에서 인텐트를 제대로 못해서 다시 인텐트를 해서 처리했습니다.

Bundle b = intent.getExtras();
 
Iterator<String> iterator = b.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = b.get(key).toString();
 
if (key.equals("msg")) {
Log.d(TAG, "onMessage. "+key+" : "+value);
}
displayMessage(context, value);
generateNotification(context, value);
}
Noswind (2,330 포인트) 님이 2014년 8월 5일 답변
0 추천
$request = array(
  'registration_ids' => $reg,
  'data' => array(
              'msg' => array(  'value1' => 'ABCDEF',
                              'value2' => '가나다라마바사'),
              'extra' => 'extra data'
              )
          );

딱봐도 이 부분이 뭔가 데이터를 보내는 부분인데, 원하는게 어떤건가요?

 

쎄미 (162,410 포인트) 님이 2014년 8월 5일 답변
2개의 값이 있어서 나머지는 잘라서 사용하였습니다 ㅎ
...