Skip to content

Commit

Permalink
fix: notification premission check (#263)
Browse files Browse the repository at this point in the history
* fix: notification premission check

* fix: rm useless condition
  • Loading branch information
greenhat616 authored Jan 14, 2024
1 parent ce0224d commit c1f8c54
Showing 1 changed file with 12 additions and 11 deletions.
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);
};

0 comments on commit c1f8c54

Please sign in to comment.