relback changes to front
This commit is contained in:
@@ -1,65 +0,0 @@
|
|||||||
// composables/usePushNotifications.ts
|
|
||||||
import { useLocalStorage, usePermission } from "@vueuse/core";
|
|
||||||
import { onMounted, ref } from "vue";
|
|
||||||
import useSubscribeNotification from "./useSubscribeNotification";
|
|
||||||
|
|
||||||
interface VapidKeys {
|
|
||||||
publicKey: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const usePushNotifications = () => {
|
|
||||||
const isSupported = ref(false);
|
|
||||||
const permission = usePermission("notifications");
|
|
||||||
const subscription = useLocalStorage<PushSubscriptionJSON | null>(
|
|
||||||
"push-subscription",
|
|
||||||
null
|
|
||||||
);
|
|
||||||
const vapid = ref<VapidKeys | null>(null);
|
|
||||||
|
|
||||||
const { mutateAsync: subscribeNotification } = useSubscribeNotification();
|
|
||||||
const toast = useToast();
|
|
||||||
|
|
||||||
// Only run in client-side
|
|
||||||
onMounted(async () => {
|
|
||||||
if (typeof window !== "undefined" && "serviceWorker" in navigator) {
|
|
||||||
isSupported.value = true;
|
|
||||||
vapid.value = await $fetch("/api/vapid");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const subscribe = async () => {
|
|
||||||
if (!isSupported.value || !vapid.value?.publicKey) {
|
|
||||||
throw new Error("Push notifications not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
const swRegistration = await navigator.serviceWorker.ready;
|
|
||||||
|
|
||||||
const applicationServerKey = vapid.value.publicKey
|
|
||||||
.replace(/-/g, "+")
|
|
||||||
.replace(/_/g, "/");
|
|
||||||
|
|
||||||
const convertedKey = Uint8Array.from(atob(applicationServerKey), (c) =>
|
|
||||||
c.charCodeAt(0)
|
|
||||||
);
|
|
||||||
|
|
||||||
const pushSubscription = await swRegistration.pushManager.subscribe({
|
|
||||||
userVisibleOnly: true,
|
|
||||||
applicationServerKey: convertedKey,
|
|
||||||
});
|
|
||||||
|
|
||||||
const subscriptionJson = pushSubscription.toJSON();
|
|
||||||
|
|
||||||
subscribeNotification({
|
|
||||||
body: subscriptionJson,
|
|
||||||
});
|
|
||||||
|
|
||||||
subscription.value = subscriptionJson;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
isSupported,
|
|
||||||
permission,
|
|
||||||
subscribe,
|
|
||||||
subscription,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@@ -8,7 +8,7 @@ definePageMeta({
|
|||||||
|
|
||||||
// imports
|
// imports
|
||||||
|
|
||||||
import { usePushNotifications } from "~/composables/api/notifications/usePushNotifications";
|
import { usePushNotifications } from "~/composables/global//usePushNotifications";
|
||||||
import useSubscribeNotification from "~/composables/api/notifications/useSubscribeNotification";
|
import useSubscribeNotification from "~/composables/api/notifications/useSubscribeNotification";
|
||||||
|
|
||||||
// state
|
// state
|
||||||
|
|||||||
Reference in New Issue
Block a user