useWebNotification
通知 API 的 Web 通知接口用於配置並向用戶顯示桌面通知。
注意
注意:對於Mac系統的用戶,需要在系統設置中啟用Chrome的通知功能。 如果系統中沒有啟用Chrome的通知,即使前端頁面請求權限成功,也不會顯示通知。
Usage
即時編輯器
function Demo() { const { isSupported, show, close } = useWebNotification(true); return ( <div> <p>支持狀態:{JSON.stringify(isSupported)}</p> {isSupported ? ( <div> <button onClick={() => { show("你好,來自 ReactUse 的問候!"); }} > 顯示通知 </button> <button onClick={close}>關閉</button> </div> ) : ( <div>您的瀏覽器不支持 Web 通知 API。</div> )} </div> ); }
結果
Loading...
API
useWebNotification
Returns
UseWebNotificationReturn
Arguments
| 參數名 | 描述 | 類型 | 預設值 |
|---|---|---|---|
| requestPermissions | 自动请求权限 | boolean | undefined | - |
UseWebNotificationReturn
| 參數名 | 描述 | 類型 | 預設值 |
|---|---|---|---|
| isSupported | 浏览器是否支持 | boolean (必填) | - |
| show | 展示函数 | UseWebNotificationShow (必填) | - |
| close | 关闭函数 | () => void (必填) | - |
| ensurePermissions | 请求权限函数 | () => Promise<boolean | undefined> (必填) | - |
| permissionGranted | 权限状态 | React.MutableRefObject<boolean> (必填) | - |
UseWebNotificationShow
Returns
Notification | undefined
Arguments
| 參數名 | 描述 | 類型 | 預設值 |
|---|---|---|---|
| title | 通知标题 | string (必填) | - |
| options | 通知选项 | NotificationOptions | undefined | - |