finish push message functionality in web client
This commit is contained in:
parent
42bcfd0a6d
commit
f3fc612c99
@ -1,18 +1,18 @@
|
|||||||
function checkServiceWorkerSupport() {
|
function checkServiceWorkerSupport() {
|
||||||
if (!"serviceWorker" in navigator || !"PushManager" in window)
|
if (!("serviceWorker" in navigator) || !("PushManager" in window))
|
||||||
throw new Error("Your browser does not have Service Worker support")
|
throw new Error("Your browser does not have Service Worker support")
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unregisterPush() {
|
async function unregisterPush() {
|
||||||
checkServiceWorkerSupport();
|
checkServiceWorkerSupport();
|
||||||
|
|
||||||
const registration = await navigator.serviceWorker.getRegistration();
|
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||||
if (!registration) return;
|
|
||||||
|
|
||||||
const subscription = await reg.pushManager.getSubscription();
|
for (const reg of registrations) {
|
||||||
if (!subscription) return;
|
const sub = await reg.pushManager.getSubscription();
|
||||||
|
if (sub) await sub.unsubscribe();
|
||||||
await subscription.unsubscribe();
|
await reg.unregister();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function registerPush(publicKey) {
|
async function registerPush(publicKey) {
|
||||||
@ -36,7 +36,7 @@ async function registerPush(publicKey) {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": localStorage.token
|
"Authorization": getToken()
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
provider: "webpush",
|
provider: "webpush",
|
||||||
@ -52,18 +52,18 @@ async function registerPush(publicKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function urlBase64ToUint8Array(base64String) {
|
async function isPushRegistered() {
|
||||||
const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
|
checkServiceWorkerSupport();
|
||||||
const base64 = (base64String + padding)
|
|
||||||
.replace(/\-/g, "+")
|
|
||||||
.replace(/_/g, "/");
|
|
||||||
|
|
||||||
const rawData = atob(base64);
|
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||||
const outputArray = new Uint8Array(rawData.length);
|
if (!registrations || registrations.length === 0) {
|
||||||
|
return false;
|
||||||
for (let i = 0; i < rawData.length; i++) {
|
|
||||||
outputArray[i] = rawData.charCodeAt(i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return outputArray;
|
for (const reg of registrations) {
|
||||||
|
const sub = await reg.pushManager.getSubscription();
|
||||||
|
if (sub) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
@ -2,6 +2,8 @@ self.addEventListener("push", (event) => {
|
|||||||
if (!event.data) return;
|
if (!event.data) return;
|
||||||
|
|
||||||
const payload = event.data.json();
|
const payload = event.data.json();
|
||||||
|
if (payload.type !== "message") return;
|
||||||
|
|
||||||
const channel = payload.data?.channel || {};
|
const channel = payload.data?.channel || {};
|
||||||
|
|
||||||
const isInGuild = channel.type === "GUILD_TEXT" || channel.type === "GUILD_VOICE";
|
const isInGuild = channel.type === "GUILD_TEXT" || channel.type === "GUILD_VOICE";
|
||||||
|
|||||||
29
assets/public/custom/utils.js
Normal file
29
assets/public/custom/utils.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
let authToken = null;
|
||||||
|
|
||||||
|
function getToken() {
|
||||||
|
return authToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
function urlBase64ToUint8Array(base64String) {
|
||||||
|
const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
|
||||||
|
const base64 = (base64String + padding)
|
||||||
|
.replace(/\-/g, "+")
|
||||||
|
.replace(/_/g, "/");
|
||||||
|
|
||||||
|
const rawData = atob(base64);
|
||||||
|
const outputArray = new Uint8Array(rawData.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < rawData.length; i++) {
|
||||||
|
outputArray[i] = rawData.charCodeAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
let proxied = window.XMLHttpRequest.prototype.setRequestHeader;
|
||||||
|
window.XMLHttpRequest.prototype.setRequestHeader = function() {
|
||||||
|
if (arguments[0] === "Authorization") {
|
||||||
|
authToken = arguments[1];
|
||||||
|
}
|
||||||
|
return proxied.apply(this, [].slice.call(arguments));
|
||||||
|
};
|
||||||
@ -16,20 +16,20 @@
|
|||||||
<div id="${SWITCH_CONTAINER_ID}"></div>
|
<div id="${SWITCH_CONTAINER_ID}"></div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
function addSwitches(container) {
|
async function addSwitches(container) {
|
||||||
container.appendChild(createSwitch(
|
container.appendChild(createSwitch(
|
||||||
"Push Notifications",
|
"Push Notifications",
|
||||||
"Sends you notifications even when you close the app",
|
"Sends you notifications even when you close the app",
|
||||||
"push-notifications",
|
"push-notifications",
|
||||||
false,
|
await isPushRegistered(),
|
||||||
async () => {
|
async (checked) => {
|
||||||
const publicKey = window.GLOBAL_ENV.VAPID_KEY;
|
const publicKey = window.GLOBAL_ENV.VAPID_KEY;
|
||||||
if (!publicKey) {
|
if (!publicKey) {
|
||||||
alert("Server has not enabled push notifications")
|
alert("Server has not enabled push notifications")
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await registerPush(publicKey);
|
checked ? await registerPush(publicKey) : await unregisterPush();
|
||||||
return true;
|
return true;
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
alert(err.message);
|
alert(err.message);
|
||||||
|
|||||||
@ -68,6 +68,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const customScripts = [
|
const customScripts = [
|
||||||
|
"/assets/custom/utils.js",
|
||||||
"/assets/custom/pushMessages.js",
|
"/assets/custom/pushMessages.js",
|
||||||
"/assets/custom/rtcPatcher.js",
|
"/assets/custom/rtcPatcher.js",
|
||||||
"/assets/custom/web/switch.js",
|
"/assets/custom/web/switch.js",
|
||||||
|
|||||||
@ -54,7 +54,7 @@ router.post(
|
|||||||
throw new HTTPError("Endpoint is not a valid url", 400);
|
throw new HTTPError("Endpoint is not a valid url", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
await webpush.sendNotification(subscription, JSON.stringify({ body: "Swoosh. Notifications are a go!" })).catch(async (err) => {
|
await webpush.sendNotification(subscription, JSON.stringify({ type: "test" })).catch(async (err) => {
|
||||||
console.error("[WebPush] Failed to send test notification:", err.message);
|
console.error("[WebPush] Failed to send test notification:", err.message);
|
||||||
throw new HTTPError("Failed to send test notification", 400);
|
throw new HTTPError("Failed to send test notification", 400);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
export interface Notification {
|
export interface Notification {
|
||||||
type: "message";
|
type: "message" | "test";
|
||||||
data: object;
|
data?: object;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user