optimize notifications
This commit is contained in:
parent
9014ce0ddf
commit
04364ec178
@ -1,3 +1,4 @@
|
|||||||
|
import { goto } from '$app/navigation';
|
||||||
|
|
||||||
export async function requestNotificationPermission() {
|
export async function requestNotificationPermission() {
|
||||||
// Check if the browser supports notifications
|
// Check if the browser supports notifications
|
||||||
@ -19,12 +20,27 @@ export async function requestNotificationPermission() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sendNotification(title: string, options?: NotificationOptions & { iconSize?: number }) {
|
|
||||||
// Only send if permission is granted
|
export function sendNotification(
|
||||||
if (Notification.permission === 'granted') {
|
title: string,
|
||||||
new Notification(title, {
|
options?: NotificationOptions & { iconSize?: number; url?: string }
|
||||||
icon: "/pwa-192x192.png", // Use the imported image directly
|
) {
|
||||||
...options
|
// Only send if permission is granted
|
||||||
});
|
if (Notification.permission === 'granted') {
|
||||||
}
|
// Extract custom properties and remaining NotificationOptions
|
||||||
|
const { iconSize, url, ...notificationOptions } = options || {};
|
||||||
|
|
||||||
|
const notification = new Notification(title, {
|
||||||
|
icon: "/pwa-192x192.png",
|
||||||
|
...notificationOptions // Spread only valid NotificationOptions
|
||||||
|
});
|
||||||
|
|
||||||
|
// Navigate when the notification is clicked
|
||||||
|
if (url) {
|
||||||
|
notification.onclick = () => {
|
||||||
|
window.focus(); // Ensure the window is focused
|
||||||
|
goto(url); // Client-side navigation
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,11 +1,13 @@
|
|||||||
// lib/workers/test.ts
|
// lib/workers/test.ts
|
||||||
|
|
||||||
async function loadNotifications() {
|
async function loadNotifications() {
|
||||||
|
const postData = {'readed': false}
|
||||||
const response = await fetch("/api/get-notifications", {
|
const response = await fetch("/api/get-notifications", {
|
||||||
method: "GET",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
body: JSON.stringify(postData)
|
||||||
});
|
});
|
||||||
|
|
||||||
const output = await response?.json() || [];
|
const output = await response?.json() || [];
|
||||||
|
|||||||
@ -51,12 +51,11 @@
|
|||||||
import AudioLine from "lucide-svelte/icons/audio-lines";
|
import AudioLine from "lucide-svelte/icons/audio-lines";
|
||||||
import Gem from "lucide-svelte/icons/gem";
|
import Gem from "lucide-svelte/icons/gem";
|
||||||
import stocknear_logo from "$lib/images/stocknear_logo.png";
|
import stocknear_logo from "$lib/images/stocknear_logo.png";
|
||||||
/*
|
|
||||||
import {
|
import {
|
||||||
requestNotificationPermission,
|
requestNotificationPermission,
|
||||||
sendNotification,
|
sendNotification,
|
||||||
} from "$lib/notifications";
|
} from "$lib/notifications";
|
||||||
*/
|
|
||||||
|
|
||||||
export let data;
|
export let data;
|
||||||
|
|
||||||
@ -96,21 +95,21 @@
|
|||||||
//pushNotification()
|
//pushNotification()
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
// Send notification and handle click redirection
|
// Send notification and handle click redirection
|
||||||
async function handleNotificationClick() {
|
async function handleNotificationClick() {
|
||||||
const permissionGranted = await requestNotificationPermission();
|
const permissionGranted = await requestNotificationPermission();
|
||||||
|
|
||||||
if (permissionGranted) {
|
if (permissionGranted) {
|
||||||
sendNotification("Stocknear", {
|
sendNotification("Stocknear", {
|
||||||
body: "This is a detailed notification message",
|
body: "Celestica shares are trading higher following better-than-expected Q4 financial results and Q1 guidance issued above estimates.",
|
||||||
iconSize: 12, // Smaller logo size
|
iconSize: 12,
|
||||||
|
url: "/notifications",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check Service Worker (SW)
|
//Check Service Worker (SW)
|
||||||
|
/*
|
||||||
async function detectSWUpdate() {
|
async function detectSWUpdate() {
|
||||||
try {
|
try {
|
||||||
const registration = await navigator.serviceWorker.ready;
|
const registration = await navigator.serviceWorker.ready;
|
||||||
@ -1216,11 +1215,10 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="w-full">
|
<div class="w-full">
|
||||||
<main class="w-full overflow-y-auto bg-default sm:p-4">
|
<main class="w-full overflow-y-auto bg-default sm:p-4">
|
||||||
<!--
|
|
||||||
<button on:click={handleNotificationClick}>
|
<button on:click={handleNotificationClick}>
|
||||||
Send Notification
|
Send Notification
|
||||||
</button>
|
</button>
|
||||||
-->
|
|
||||||
<slot />
|
<slot />
|
||||||
<Toaster class="bg-[#1A1A27] text-white text-medium" />
|
<Toaster class="bg-[#1A1A27] text-white text-medium" />
|
||||||
{#if Cookie && $showCookieConsent === true}
|
{#if Cookie && $showCookieConsent === true}
|
||||||
|
|||||||
@ -1,16 +1,26 @@
|
|||||||
import type { RequestHandler } from "./$types";
|
import type { RequestHandler } from "./$types";
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ locals }) => {
|
export const POST: RequestHandler = async ({ locals, request }) => {
|
||||||
const { pb, user } = locals;
|
const { pb, user } = locals;
|
||||||
|
const data = await request.json();
|
||||||
let output;
|
let output;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
output = await pb?.collection("notifications")?.getFullList({
|
|
||||||
filter: `opUser="${user?.id}" `,
|
if (data?.readed === false) {
|
||||||
|
output = await pb?.collection("notifications")?.getFullList({
|
||||||
|
filter: `opUser="${user?.id}" && readed=False`,
|
||||||
expand: "user",
|
expand: "user",
|
||||||
sort: "-created",
|
sort: "-created",
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
output = await pb?.collection("notifications")?.getFullList({
|
||||||
|
filter: `opUser="${user?.id}"`,
|
||||||
|
expand: "user",
|
||||||
|
sort: "-created",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
output = [];
|
output = [];
|
||||||
|
|||||||
@ -8,11 +8,14 @@ export const load = async ({ locals, fetch }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getNotifications() {
|
async function getNotifications() {
|
||||||
|
|
||||||
|
const postData = {'readed': true}
|
||||||
const response = await fetch("/api/get-notifications", {
|
const response = await fetch("/api/get-notifications", {
|
||||||
method: "GET",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
body: JSON.stringify(postData),
|
||||||
});
|
});
|
||||||
|
|
||||||
const output = await response.json();
|
const output = await response.json();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user