Notification Section -> Setting

This commit is contained in:
JonatanRek 2019-10-11 10:44:13 +02:00
parent 669b873a4f
commit 15c429e151
3 changed files with 68 additions and 7 deletions

View File

@ -0,0 +1,40 @@
navigator.permissions.query({name:'notifications'}).then(function(result) {
var element = document.getElementById("notifications");
if (result.state === 'granted') {
element.checked = true;
} else if (result.state === 'denied') {
element.checked = false;
} else if (result.state === 'prompt') {
element.checked = false;
}
});
function toggleNotificationPermissions(input){
navigator.permissions.query({name:'notifications'}).then(function(result) {
if (result.state === 'granted') {
input.checked = true;
} else if (result.state === 'denied') {
input.checked = false;
} else if (result.state === 'prompt') {
input.checked = false;
}
});
}
function sendTestNotification(){
console.log("sending test notification");
$.ajax({
url: 'ajax',
type: 'POST',
data: {
"notification" : 'X',
"action": 'sendTest'
},
success: function(data){
console.log(data);
},
error: function (request, status, error) {
console.log("ERROR ", request, error);
}
});
}

View File

@ -20,10 +20,10 @@
</div> </div>
<div class="col-md-9 main-body"> <div class="col-md-9 main-body">
<div class="col-12 col-sm-9 mx-auto mt-4"> <div class="col-12 col-sm-9 mx-auto mt-4">
<h4 class="mb-4">Stránka po načtení</h4>
<form method="post" enctype="multipart/form-data"> <form method="post" enctype="multipart/form-data">
<div class=""> <div class="">
<div class="field"> <div class="field">
<div class="label">Stránka po načtení</div>
<select class="input" name="loadPage"> <select class="input" name="loadPage">
<option value="0" <?php echo (UserManager::getUserData("startPage") == 0 ? "selected" : ""); ?>>Default</option> <option value="0" <?php echo (UserManager::getUserData("startPage") == 0 ? "selected" : ""); ?>>Default</option>
<option value="1" <?php echo (UserManager::getUserData("startPage") == 1 ? "selected" : ""); ?>>Dashboard</option> <option value="1" <?php echo (UserManager::getUserData("startPage") == 1 ? "selected" : ""); ?>>Dashboard</option>
@ -34,26 +34,30 @@
</div> </div>
</form> </form>
<div class="col-12 col-sm-9 mx-auto mt-4"> <div class="col-12 col-sm-9 mx-auto mt-4">
<h4 class="mb-4">Profil</h4>
<div class="field"> <div class="field">
<a href="logout" class="button is-primary">Odhlásit se</a> <a href="logout" class="button is-primary">Odhlásit se</a>
</div> </div>
</div> </div>
<div class="col-12 col-sm-9 mx-auto mt-4"> <div class="col-12 col-sm-9 mx-auto mt-4">
<div class="label">Testovavcí Nastavení</div> <h4 class="mb-4">Notifications</h4>
<input id="notifications" type="checkbox" onChange="toggleNotificationPermissions(this);" />
<div class="label">Status notifikací</div>
<div class="field">
<a onClick="sendTestNotification();" class="button">Test Notification</a>
</div>
</div>
<div class="col-12 col-sm-9 mx-auto mt-4">
<h4 class="mb-4">Logs</h4>
<div class="field"> <div class="field">
<a href="rooms" class="button">ROOMS</a> <a href="rooms" class="button">ROOMS</a>
</div> </div>
</div> </div>
<div class="col-12 col-sm-9 mx-auto mt-4">
<div class="field">
<a href="log" class="button">Logs</a>
</div>
</div>
</div> </div>
</div> </div>
<script src="./app/templates/js/setting.js"></script>
<?php <?php
$partial = new Partial('footer'); $partial = new Partial('footer');
$partial->render(); $partial->render();

View File

@ -68,7 +68,7 @@ class Ajax extends Template
if ($subDeviceData['type'] == 'on/off'){ if ($subDeviceData['type'] == 'on/off'){
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value']; $lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
RecordManager::create($deviceId, 'on/off', !$lastValue); RecordManager::create($deviceId, 'on/off', !$lastValue);
echo ($lastValue ? 'ON' : 'OFF'); echo (!$lastValue ? 'ON' : 'OFF');
} }
die(); die();
break; break;
@ -114,11 +114,28 @@ class Ajax extends Template
switch ($_POST['action']) { switch ($_POST['action']) {
//add suscription to database //add suscription to database
case 'subscribe': case 'subscribe':
$subscriptionToken = $_POST['Token']; $subscriptionToken = $_POST['token'];
NotificationManager::addSubscriber($_SESSION['user']['id'], $subscriptionToken); NotificationManager::addSubscriber($_SESSION['user']['id'], $subscriptionToken);
die(); die();
break; break;
case 'sendTest':
echo "test";
$notificationData = [
'title' => 'Alert',
'body' => 'test notification',
'icon' => '',
];
$notificationMng = new NotificationManager;
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
echo $subscriber['user_id'];
if ($subscriber['user_id'] != $_SESSION['user']['id']) continue;
echo $notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
die();
break;
default: default:
echo 'no action detected'; echo 'no action detected';
break; break;