mirror of
https://github.com/zoldar/jenot.git
synced 2026-01-05 07:02:55 +00:00
Test notifications API
This commit is contained in:
parent
599004206b
commit
47f06b2a2a
3 changed files with 47 additions and 1 deletions
|
|
@ -30,6 +30,9 @@
|
||||||
<div id="content">
|
<div id="content">
|
||||||
<h1>Jenot</h1>
|
<h1>Jenot</h1>
|
||||||
|
|
||||||
|
<button id="enable-notifications" class="hidden"">Enable notifications</button>
|
||||||
|
<button id="test-notifications">Send test notification</button>
|
||||||
|
|
||||||
<note-form mode="add" id="new-note">
|
<note-form mode="add" id="new-note">
|
||||||
<div class="note">
|
<div class="note">
|
||||||
<p class="content">
|
<p class="content">
|
||||||
|
|
@ -37,7 +40,7 @@
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<button class="add">➕</button>
|
<button class="add">➕</button>
|
||||||
<button class="tasklist-mode">☑️</button>
|
<button class="tasklist-mode">☑️</button>
|
||||||
<button class="note-mode">📑</button>
|
<button class="note-mode hidden">📑</button>
|
||||||
<button class="remove">🗑️</button>
|
<button class="remove">🗑️</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
21
js/jenot.js
21
js/jenot.js
|
|
@ -2,8 +2,29 @@ import "./service-worker.js";
|
||||||
import { renderText } from "./dom.js";
|
import { renderText } from "./dom.js";
|
||||||
import { NoteStore } from "./store.js";
|
import { NoteStore } from "./store.js";
|
||||||
import { DBNoteStore } from "./db-store.js";
|
import { DBNoteStore } from "./db-store.js";
|
||||||
|
import {
|
||||||
|
authorizeNotifications,
|
||||||
|
notificationsEnabled,
|
||||||
|
sendNotification,
|
||||||
|
} from "./notifications.js";
|
||||||
import "./components.js";
|
import "./components.js";
|
||||||
|
|
||||||
|
const notificationsButton = document.querySelector("#enable-notifications");
|
||||||
|
const notificationsTestButton = document.querySelector("#test-notifications");
|
||||||
|
|
||||||
|
if (!notificationsEnabled()) {
|
||||||
|
notificationsButton.classList.remove("hidden");
|
||||||
|
notificationsButton.addEventListener("click", () => {
|
||||||
|
authorizeNotifications(() => notificationsButton.classList.add("hidden"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
notificationsTestButton.addEventListener("click", () => {
|
||||||
|
setTimeout(() => {
|
||||||
|
sendNotification("reminder", "This is a test reminder!");
|
||||||
|
}, 8000);
|
||||||
|
});
|
||||||
|
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
const Notes = urlParams.has("localStorage")
|
const Notes = urlParams.has("localStorage")
|
||||||
|
|
|
||||||
22
js/notifications.js
Normal file
22
js/notifications.js
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
const notificationIcon = "/img/android-chrome-192x192.png";
|
||||||
|
|
||||||
|
export function notificationsEnabled() {
|
||||||
|
return Notification.permission === "granted";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function authorizeNotifications(afterCallback) {
|
||||||
|
Notification.requestPermission(function (permission) {
|
||||||
|
sendNotification(
|
||||||
|
"notification",
|
||||||
|
"Jenot can now send you alerts about reminders!",
|
||||||
|
);
|
||||||
|
afterCallback();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sendNotification(titleSuffix, message) {
|
||||||
|
return new Notification(`Jenot ${titleSuffix}`, {
|
||||||
|
body: message,
|
||||||
|
icon: notificationIcon,
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue