feat/push-notifications #34

Merged
retoor merged 16 commits from :feat/push-notifications into main 2025-06-06 11:25:09 +02:00
4 changed files with 79 additions and 41 deletions
Showing only changes of commit 1a26cacb66 - Show all commits

View File

@ -11,8 +11,11 @@ function arrayBufferToBase64(buffer) {
const keyResponse = await fetch('/push.json')
Outdated
Review

In the future this can be replaced with Uint8Array.fromBase64() It's just not available in chrome/old firefox ;P

In the future this can be replaced with `Uint8Array.fromBase64()` It's just not available in chrome/old firefox ;P

Then it's easy, i prefer this. Because let's face it, that frombase64 code doesn't do so much. Don't see a reason to exclude browsers for such simple functionality.

Then it's easy, i prefer this. Because let's face it, that frombase64 code doesn't do so much. Don't see a reason to exclude browsers for such simple functionality.
const keyData = await keyResponse.json()
console.log("Key data", keyData);
const publicKey = Uint8Array.from(atob(keyData.publicKey), c => c.charCodeAt(0))
export const registerServiceWorker = async () => {
navigator.serviceWorker
.register("/service-worker.js")
.then((serviceWorkerRegistration) => {
@ -52,3 +55,17 @@ navigator.serviceWorker
},
Outdated
Review

Some browsers will show the permission prompt immediately, others require a "registered" interaction

Some browsers will show the permission prompt immediately, others require a "registered" interaction
);
});
}
window.registerServiceWorker = () => {
return Notification.requestPermission().then((permission) => {
if (permission === "granted") {
return registerServiceWorker();
} else if (permission === "denied") {
console.log("Permission was denied");
} else {
console.log("Permission was dismissed");
}
});
};
registerServiceWorker().catch(console.error);

View File

@ -7,7 +7,6 @@ async function requestNotificationPermission() {
async function subscribeUser() {
const registration =
await navigator.serviceWorker.register("/service-worker.js");
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(PUBLIC_VAPID_KEY),
@ -44,12 +43,15 @@ self.addEventListener("push", (event) => {
const message =
data.message || "Here's something you might want to check out.";
const icon = "images/new-notification.png";
console.log("showing message", title, message, icon);
event.waitUntil(self.registration.showNotification(title, {
const reg = self.registration.showNotification(title, {
body: message,
tag: "simple-push-demo-notification",
icon,
}));
}).then(e => console.log("success", e)).catch(console.error);
event.waitUntil(reg);
Outdated
Review

Only added because somewhere while reading this was a requirement on some browsers to get push notifications to work with WPAs

Only added because somewhere while reading this was a requirement on some browsers to get push notifications to work with WPAs
});
@ -59,3 +61,21 @@ self.addEventListener("notificationclick", (event) => {
event.waitUntil(clients.openWindow(
"https://snek.molodetz.nl",));
});
self.addEventListener("notificationclose", (event) => {
})
self.addEventListener("fetch", (event) => {
// console.log("Fetch event for ", event.request.url);
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
// console.log("Found response in cache: ", response);
return response;
}
// console.log("No response found in cache. About to fetch from network...");
return fetch(event.request);
})
);
})

View File

@ -41,6 +41,7 @@
<a class="no-select" style="display:none" id="install-button" href="#">📥</a>
<a class="no-select" href="/threads.html">👥</a>
<a class="no-select" href="/settings/index.html">⚙️</a>
<a class="no-select" href="#" onclick="registerServiceWorker">✉️</a>
<a class="no-select" href="/logout.html">🔒</a>
</nav>

View File

@ -65,7 +65,7 @@ class PushView(BaseFormView):
print(body)
notifications =get_notifications()
cert = base64.b64encode(
cert = base64.urlsafe_b64encode(
notifications.public_key.public_bytes(
encoding=serialization.Encoding.X962,
format=serialization.PublicFormat.UncompressedPoint