2013년 7월 31일 수요일

GSM - Android Push

인터넷에서 발견한 코드인데 보관용으로 적어둔다.
웹은 별로 해보지 않은터라, 요즘 웹에도 관심이 간다.
curl 명령어로 문자열 처리가 간단하게 된다.


Link :: http://www.androidbegin.com/tutorial/android-google-cloud-messaging-gcm-tutorial/


## Server

gcm_engine.php

<?php
// Message to be sent
$message = $_POST['message'];
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
                'registration_ids'  => array($_POST['registrationIDs']),
                'data'              => array( "message" => $message ),
                );
$headers = array(
                    'Authorization: key=' . $_POST['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);
echo $result;
?>

gcm.html

<html>
<head>
<title>AndroidBegin - Android GCM Tutorial</title>
<link rel="icon" type="image/png" href="http://www.androidbegin.com/wp-content/uploads/2013/04/favicon1.png"/>
</head>
<body>
<a href="http://www.AndroidBegin.com" target="_blank">
<img src="http://www.androidbegin.com/wp-content/uploads/2013/04/Web-Logo.png" alt="AndroidBegin.com"></br></a></br>
<form action="gcm_engine.php" method="post">
Google API Key (with IP locking) : <INPUT size=70% TYPE="Text" VALUE="" NAME="apiKey"></br>
Get your Google API Key : <a href="https://code.google.com/apis/console/" target="_blank">Google API</a></br></br>
<img src="http://www.androidbegin.com/wp-content/uploads/2013/05/Google-API-Key.png" alt="Google API Key" ></br></br>
Device Registration ID : <INPUT size=70% TYPE = "Text" VALUE="" NAME = "registrationIDs"></br></br>
Tap on the Register button in your GCM Tutorial App and locate Device Registration ID in LogCat</br>
<img src="http://www.androidbegin.com/wp-content/uploads/2013/05/Device-Registration-ID.png" alt="Device Registration ID" ></br></br>
Notification Message : <INPUT size=70% TYPE = "Text" VALUE="" NAME = "message"></br></br>
<input type="submit" value="Send Notification"/>
</form>
</body>
</html>

댓글 없음:

댓글 쓰기