diff --git a/app/templates/js/setting.js b/app/templates/js/setting.js new file mode 100644 index 0000000..16614c8 --- /dev/null +++ b/app/templates/js/setting.js @@ -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); + } + }); +} \ No newline at end of file diff --git a/app/templates/setting.phtml b/app/templates/setting.phtml index 593ad91..ce790d7 100644 --- a/app/templates/setting.phtml +++ b/app/templates/setting.phtml @@ -20,10 +20,10 @@
+

Stránka po načtení

-
Stránka po načtení
+
Status notifikací
+

Logs

- Logs + ROOMS
- + render(); diff --git a/app/views/Ajax.php b/app/views/Ajax.php index 52f6189..6b12500 100644 --- a/app/views/Ajax.php +++ b/app/views/Ajax.php @@ -68,7 +68,7 @@ class Ajax extends Template if ($subDeviceData['type'] == 'on/off'){ $lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value']; RecordManager::create($deviceId, 'on/off', !$lastValue); - echo ($lastValue ? 'ON' : 'OFF'); + echo (!$lastValue ? 'ON' : 'OFF'); } die(); break; @@ -114,11 +114,28 @@ class Ajax extends Template switch ($_POST['action']) { //add suscription to database case 'subscribe': - $subscriptionToken = $_POST['Token']; + $subscriptionToken = $_POST['token']; NotificationManager::addSubscriber($_SESSION['user']['id'], $subscriptionToken); die(); 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: echo 'no action detected'; break;