Last active
March 4, 2016 14:49
-
-
Save blackcj/5976493 to your computer and use it in GitHub Desktop.
Used to send test messages through Google Cloud Messaging to an Android app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<title>GCM Sender</title> | |
</head> | |
<body> | |
<h2>GCM Sender</h2> | |
<form method="POST" name="input" action="gcm_sender.php" /> | |
Device Token:<input name="deviceToken" /> <br /> | |
API Key:<input name="apiKey" /> <br /> | |
Message:<input name="message" /> <br /> | |
Collapse Key:<input name="collapseKey" /> <br /> | |
<input type="submit" value="Send Message"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sendMessageToPhone() | |
{ | |
$deviceToken = $_POST['deviceToken']; // Returned from GCM when a user opens the app, device specific | |
$messageText = $_POST['message']; // Message that will be send to the user | |
$collapseKey = $_POST['collapseKey']; // Key used to group messages, for example '123' | |
$yourKey = $_POST['apiKey']; // Retrieve from https://code.google.com/apis/console/ | |
$headers = array('Authorization:key=' . $yourKey); | |
$data = array( | |
'to' => $deviceToken, | |
'collapse_key' => $collapseKey, | |
'data.message' => $messageText, | |
'delay_while_idle' => false, | |
'time_to_live' => 108, | |
'message_id' => uniqid('nerd_')); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send"); | |
if ($headers) | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
$response = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if (curl_errno($ch)) { | |
//request failed | |
return false;//probably you want to return false | |
} | |
if ($httpCode != 200) { | |
//request failed | |
return false;//probably you want to return false | |
} | |
curl_close($ch); | |
$arr = array ('response'=>$response); | |
echo json_encode($arr); | |
return $response; | |
} | |
if (isset($_POST['deviceToken'])) { | |
sendMessageToPhone(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment