Skip to content

Instantly share code, notes, and snippets.

@zirouan
Last active October 2, 2015 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zirouan/3ab8d4693dc82dedd000 to your computer and use it in GitHub Desktop.
Save zirouan/3ab8d4693dc82dedd000 to your computer and use it in GitHub Desktop.
Simples serviço de registro de Ids GCM
<?php
$acao = $_POST['acao'];
$dispositivos = "dispositivos.txt";
if ($acao == "registrar") {
$fp = fopen($dispositivos, "a+") or die("Erro ao abrir arquivo!");
$registrationId = $_POST['regId'] ."\n";
fwrite($fp, $registrationId) or die("Erro ao escrever no arquivo!");
fclose($fp);
} else if ($acao == "enviar") {
$mensagem = $_POST['mensagem'];
$mensagem_utf8 = utf8_encode($mensagem);
if (file_exists($dispositivos)) {
$linhas = file($dispositivos, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
$data = array(
"registration_ids" => $linhas,
"data" => array (
"mensagem" => $mensagem_utf8
)
);
$NL = "\r\n";
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json". $NL.
"Authorization: key=COLOQUE_SUA_SENDER_AUTH_TOKEN" . $NL
)
);
$url = "https://android.googleapis.com/gcm/send";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment