Skip to content

Instantly share code, notes, and snippets.

@thuliumsystems
Created May 12, 2020 17:12
Show Gist options
  • Save thuliumsystems/77c68a568d81473becd932941d134164 to your computer and use it in GitHub Desktop.
Save thuliumsystems/77c68a568d81473becd932941d134164 to your computer and use it in GitHub Desktop.
<?php
//no login, o id do aparelho é enviado ao banco
if (isset($post['pushid'])) {
//insere no banco o id do aparelho
$query = "UPDATE login SET push_id = '" . $post['pushid'] . "' WHERE id_login = " . $usuario->id_login;
$push_conn->query($query);
//isso é opcional, é para atualizar no site do onesignal o grupo do usuário
$playerID = $post['pushid'];
$fields = array(
'app_id' => '',
'tags' => array('condo' => "$condo")
);
$fields2 = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/players/' . $playerID);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
}
//quando algum evento é disparado, o push é enviado ao aparelho do usuário
function fn_push_reserva_confirmada($pushId) {
$fields = array(
'app_id' => "",
'include_player_ids' => array($pushId),
//'headings' => array("en" => "Reserva"),
'contents' => array("en" => "Reserva confirmada.")
);
fn_send_push($fields);
}
function fn_send_push($fields) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic '));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_exec($ch);
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment