-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
322 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# useWebNotification | ||
|
||
The Web Notification interface of the Notifications API is used to configure and display desktop notifications to the user. | ||
|
||
::: warning | ||
Attention: For users on the Mac system, it is necessary for them to have enabled notifications for Chrome in their system settings. Without enabling notifications for Chrome in the system, even if the front-end page requests permission, notifications will not be displayed. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import useWebNotification from "."; | ||
|
||
export default function Demo() { | ||
const { isSupported, show, close } | ||
= useWebNotification(true); | ||
return ( | ||
<div> | ||
<p>Supported: {JSON.stringify(isSupported)}</p> | ||
{isSupported | ||
? ( | ||
<div> | ||
<button | ||
onClick={() => { | ||
show("Hello, world from ReactUse!"); | ||
}} | ||
> | ||
Show Notification | ||
</button> | ||
<button onClick={close}>Close</button> | ||
</div> | ||
) | ||
: ( | ||
<div>The Notification Web API is not supported in your browser.</div> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import useWebNotification from "."; | ||
|
||
describe("useWebNotification", () => { | ||
it("should be defined", () => { | ||
expect(useWebNotification).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useCallback, useEffect, useRef } from "react"; | ||
import useSupported from "../useSupported"; | ||
import useUnmount from "../useUnmount"; | ||
import { defaultOptions } from "../utils/defaults"; | ||
|
||
export default function useWebNotification(requestPermissions = false) { | ||
const isSupported = useSupported(() => !!window && "Notification" in window); | ||
const permissionGranted = useRef(false); | ||
|
||
const notificationRef = useRef<Notification | null>(null); | ||
|
||
const show = ( | ||
title: string, | ||
options: NotificationOptions = defaultOptions, | ||
) => { | ||
// If either the browser does not support notifications or the user has | ||
// not granted permission, do nothing: | ||
if (!isSupported && !permissionGranted.current) { | ||
return; | ||
} | ||
|
||
notificationRef.current = new Notification(title || "", options); | ||
return notificationRef.current; | ||
}; | ||
|
||
const close = useCallback((): void => { | ||
if (notificationRef.current) { | ||
notificationRef.current.close(); | ||
} | ||
|
||
notificationRef.current = null; | ||
}, []); | ||
|
||
useEffect(() => { | ||
permissionGranted.current | ||
= isSupported | ||
&& "permission" in Notification | ||
&& Notification.permission === "granted"; | ||
}, [isSupported]); | ||
|
||
const ensurePermissions = useCallback(async () => { | ||
if (!isSupported) | ||
return; | ||
|
||
if (!permissionGranted.current && Notification.permission !== "denied") { | ||
const result = await Notification.requestPermission(); | ||
if (result === "granted") | ||
permissionGranted.current = true; | ||
} | ||
|
||
return permissionGranted.current; | ||
}, [isSupported]); | ||
|
||
useEffect(() => { | ||
if (requestPermissions) { | ||
ensurePermissions(); | ||
} | ||
}, [requestPermissions, ensurePermissions]); | ||
|
||
useUnmount(close); | ||
|
||
return { | ||
isSupported, | ||
show, | ||
close, | ||
ensurePermissions, | ||
permissionGranted, | ||
} as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.