2021-05-06 16:39:59 +00:00
|
|
|
import apolloProvider from "@/vue-apollo";
|
2021-05-12 16:10:07 +00:00
|
|
|
import { NormalizedCacheObject } from "@apollo/client/cache/inmemory/types";
|
|
|
|
import { ApolloClient } from "@apollo/client/core/ApolloClient";
|
2021-05-06 16:39:59 +00:00
|
|
|
import { WEB_PUSH } from "../graphql/config";
|
|
|
|
import { IConfig } from "../types/config.model";
|
|
|
|
|
|
|
|
function urlBase64ToUint8Array(base64String: string): Uint8Array {
|
|
|
|
const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
|
|
|
|
const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/");
|
|
|
|
|
|
|
|
const rawData = window.atob(base64);
|
|
|
|
const outputArray = new Uint8Array(rawData.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < rawData.length; ++i) {
|
|
|
|
outputArray[i] = rawData.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return outputArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function subscribeUserToPush(): Promise<PushSubscription | null> {
|
2021-05-21 13:37:41 +00:00
|
|
|
const client =
|
|
|
|
apolloProvider.defaultClient as ApolloClient<NormalizedCacheObject>;
|
2021-05-06 16:39:59 +00:00
|
|
|
|
|
|
|
const registration = await navigator.serviceWorker.ready;
|
2021-06-28 07:36:29 +00:00
|
|
|
const { data } = await client.query<{ config: IConfig }>({
|
|
|
|
query: WEB_PUSH,
|
2021-05-06 16:39:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (data?.config?.webPush?.enabled && data?.config?.webPush?.publicKey) {
|
|
|
|
const subscribeOptions = {
|
|
|
|
userVisibleOnly: true,
|
|
|
|
applicationServerKey: urlBase64ToUint8Array(
|
|
|
|
data?.config?.webPush?.publicKey
|
|
|
|
),
|
|
|
|
};
|
2021-06-24 09:17:30 +00:00
|
|
|
try {
|
|
|
|
const pushSubscription = await registration.pushManager.subscribe(
|
|
|
|
subscribeOptions
|
|
|
|
);
|
|
|
|
console.debug("Received PushSubscription: ", pushSubscription);
|
|
|
|
return pushSubscription;
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error while subscribing to push notifications", e);
|
|
|
|
}
|
2021-05-06 16:39:59 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function unsubscribeUserToPush(): Promise<string | undefined> {
|
|
|
|
console.log("performing unsubscribeUserToPush");
|
|
|
|
const registration = await navigator.serviceWorker.ready;
|
|
|
|
console.log("found registration", registration);
|
2021-07-21 10:11:59 +00:00
|
|
|
const subscription = await registration.pushManager?.getSubscription();
|
2021-05-06 16:39:59 +00:00
|
|
|
console.log("found subscription", subscription);
|
|
|
|
if (subscription && (await subscription?.unsubscribe()) === true) {
|
|
|
|
console.log("done unsubscription");
|
|
|
|
return subscription?.endpoint;
|
|
|
|
}
|
|
|
|
console.log("went wrong");
|
|
|
|
return undefined;
|
|
|
|
}
|