Created
February 5, 2016 19:43
-
-
Save DouglasHennrich/de591567483d7292a841 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<? | |
session_start(); | |
include '../config/conecta.php'; | |
$titulo = $_POST['titulo']; | |
$descricao = $_POST['descricao']; | |
$cidade = $_POST['cidade']; | |
$nome_empresa = $_SESSION["nome"]; | |
$id_empresa = $_SESSION["id"]; | |
mysql_query("insert into tb_push (titulo,descricao,nome_empresa,id_empresa) VALUES ('$titulo','$descricao','$nome_empresa','$id_empresa')"); | |
$id_push = mysql_insert_id(); | |
$sql = mysql_query("select * from tb_favoritos where id_empresa = '$id_empresa'") or die("erro1"); | |
$dados = mysql_fetch_object($sql); | |
$array_token = $dados->token; | |
$montaArray = json_decode($array_token, true); | |
print("<pre>".print_r($montaArray,true)."</pre>"); | |
// iOS | |
$apnsHost = 'gateway.push.apple.com'; | |
$apnsPort = 2195; | |
$apnsCert = 'apns-prod.pem'; | |
// | |
$abrirPush = 1; | |
$mensagem = array( | |
"id_push" => $id_push, | |
"id_empresa" => $id_empresa, | |
"abrir" => $abrirPush | |
); | |
$mensagem = json_encode($mensagem); | |
$arr['aps'] = array( | |
'alert' => $nome_empresa . ' tem uma mensagem para você' , | |
'badge' => 1, | |
'sound' => 'default', | |
'body' => $mensagem | |
); | |
$payload = json_encode($arr); | |
// Abre Conexão iOS | |
$streamContext = stream_context_create(); | |
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert); | |
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext); | |
// Android | |
$arrayAndroid = Array(); | |
// Search for iOS or Android Device | |
for ($i = 0; $i < count($montaArray); $i++){ | |
$pegaToken = $montaArray[$i]; | |
$contaToken = strlen($pegaToken); | |
if ($contaToken < 70){ | |
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $montaArray[$i])) . chr(0) . chr(strlen($payload)) . $payload; | |
fwrite($apns, $apnsMessage); | |
}else{ | |
/// PUSH ANDROI //// | |
array_push( $arrayAndroid, $montaArray[$i]); | |
} | |
} | |
// fclose($apns); | |
fclose($apns); | |
// API access key from Google API's Console ( Server KEY ) | |
define( 'API_ACCESS_KEY', 'xxxx' ); | |
// prep the bundle | |
$msg = array | |
( | |
'message' => 'Tem uma mensagem para você', | |
'title' => $nome_empresa, | |
'payload' => $mensagem, | |
'vibrate' => 1, | |
'sound' => 1, | |
'largeIcon' => 'large_icon', | |
'smallIcon' => 'small_icon' | |
); | |
$fields = array | |
( | |
'registration_ids' => $arrayAndroid, | |
'data' => $msg | |
); | |
$headers = array | |
( | |
'Authorization: key=' . API_ACCESS_KEY, | |
'Content-Type: application/json' | |
); | |
$ch = curl_init(); | |
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' ); | |
curl_setopt( $ch,CURLOPT_POST, true ); | |
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); | |
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); | |
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); | |
$result = curl_exec($ch ); | |
print $result; | |
curl_close( $ch ); | |
echo "<script> | |
alert('Mensagem enviada com sucesso'); | |
document.location='../favoritos/'; | |
</script>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment