commented push notif file that i added (its a place holder file)
This commit is contained in:
@@ -8,58 +8,58 @@ interface VapidKeys {
|
||||
}
|
||||
|
||||
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 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();
|
||||
// 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");
|
||||
}
|
||||
});
|
||||
// // 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 subscribe = async () => {
|
||||
// if (!isSupported.value || !vapid.value?.publicKey) {
|
||||
// throw new Error("Push notifications not supported");
|
||||
// }
|
||||
|
||||
const swRegistration = await navigator.serviceWorker.ready;
|
||||
// const swRegistration = await navigator.serviceWorker.ready;
|
||||
|
||||
const applicationServerKey = vapid.value.publicKey
|
||||
.replace(/-/g, "+")
|
||||
.replace(/_/g, "/");
|
||||
// const applicationServerKey = vapid.value.publicKey
|
||||
// .replace(/-/g, "+")
|
||||
// .replace(/_/g, "/");
|
||||
|
||||
const convertedKey = Uint8Array.from(atob(applicationServerKey), (c) =>
|
||||
c.charCodeAt(0)
|
||||
);
|
||||
// const convertedKey = Uint8Array.from(atob(applicationServerKey), (c) =>
|
||||
// c.charCodeAt(0)
|
||||
// );
|
||||
|
||||
const pushSubscription = await swRegistration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: convertedKey,
|
||||
});
|
||||
// const pushSubscription = await swRegistration.pushManager.subscribe({
|
||||
// userVisibleOnly: true,
|
||||
// applicationServerKey: convertedKey,
|
||||
// });
|
||||
|
||||
const subscriptionJson = pushSubscription.toJSON();
|
||||
// const subscriptionJson = pushSubscription.toJSON();
|
||||
|
||||
subscribeNotification({
|
||||
body: subscriptionJson,
|
||||
});
|
||||
// subscribeNotification({
|
||||
// body: subscriptionJson,
|
||||
// });
|
||||
|
||||
subscription.value = subscriptionJson;
|
||||
};
|
||||
// subscription.value = subscriptionJson;
|
||||
// };
|
||||
|
||||
return {
|
||||
isSupported,
|
||||
permission,
|
||||
subscribe,
|
||||
subscription,
|
||||
};
|
||||
// return {
|
||||
// isSupported,
|
||||
// permission,
|
||||
// subscribe,
|
||||
// subscription,
|
||||
// };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user