서버에서 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 파일부분입니다.