Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: notification premission check #263

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/hooks/use-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ let permissionGranted: boolean | null = null;
const checkPermission = async () => {
if (permissionGranted == null) {
permissionGranted = await isPermissionGranted();
} else if (permissionGranted == false) {
}
if (!permissionGranted) {
const permission = await requestPermission();
permissionGranted = permission === "granted";
return permissionGranted;
} else {
return permissionGranted;
}
return permissionGranted;
};

export type NotificationOptions = {
Expand All @@ -39,15 +38,17 @@ export const useNotification = async ({
}: NotificationOptions) => {
if (!title) {
throw new Error("missing message argument!");
} else if (!checkPermission()) {
}
const permissionGranted = await checkPermission();
if (!permissionGranted) {
// fallback to mui notification
Notice[type](`${title} ${body ? `: ${body}` : ""}}`);
// throw new Error("notification permission not granted!");
} else {
const options: Options = {
title: title,
};
if (body) options.body = body;
sendNotification(options);
return;
}
const options: Options = {
title: title,
};
if (body) options.body = body;
sendNotification(options);
};
Loading