NotificationAPI: Correctly request browser permissions

This commit is contained in:
Vendicated 2023-02-14 19:20:10 +01:00
parent 2b0c25b45c
commit 68055977d2
No known key found for this signature in database
GPG Key ID: A1DC0CFB5615D905
2 changed files with 16 additions and 2 deletions

@ -76,8 +76,15 @@ function shouldBeNative() {
return false; return false;
} }
export function showNotification(data: NotificationData) { export async function requestPermission() {
if (shouldBeNative()) { return (
Notification.permission === "granted" ||
(Notification.permission !== "denied" && (await Notification.requestPermission()) === "granted")
);
}
export async function showNotification(data: NotificationData) {
if (shouldBeNative() && await requestPermission()) {
const { title, body, icon, image, onClick = null, onClose = null } = data; const { title, body, icon, image, onClick = null, onClose = null } = data;
const n = new Notification(title, { const n = new Notification(title, {
body, body,

@ -21,6 +21,7 @@ import { useSettings } from "@api/settings";
import { classNameFactory } from "@api/Styles"; import { classNameFactory } from "@api/Styles";
import DonateButton from "@components/DonateButton"; import DonateButton from "@components/DonateButton";
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
import { ErrorCard } from "@components/ErrorCard";
import IpcEvents from "@utils/IpcEvents"; import IpcEvents from "@utils/IpcEvents";
import { Margins } from "@utils/margins"; import { Margins } from "@utils/margins";
import { identity, useAwaiter } from "@utils/misc"; import { identity, useAwaiter } from "@utils/misc";
@ -141,6 +142,12 @@ function VencordSettings() {
<Forms.FormTitle tag="h5">Notification Style</Forms.FormTitle> <Forms.FormTitle tag="h5">Notification Style</Forms.FormTitle>
{notifSettings.useNative !== "never" && Notification.permission === "denied" && (
<ErrorCard style={{ padding: "1em" }} className={Margins.bottom8}>
<Forms.FormTitle tag="h5">Desktop Notification Permission denied</Forms.FormTitle>
<Forms.FormText>You have denied Notification Permissions. Thus, Desktop notifications will not work!</Forms.FormText>
</ErrorCard>
)}
<Forms.FormText className={Margins.bottom8}> <Forms.FormText className={Margins.bottom8}>
Some plugins may show you notifications. These come in two styles: Some plugins may show you notifications. These come in two styles:
<ul> <ul>