Skip to main content

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.

Usage

Live Editor

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>
  );
}

Result
Loading...

API

useWebNotification

Returns

UseWebNotificationReturn

Arguments

ArgumentDescriptionTypeDefaultValue
requestPermissionsauto request permissionboolean | undefined-

UseWebNotificationReturn

PropertyDescriptionTypeDefaultValue
isSupportedwhether browser supportboolean (Required)-
showshow functionUseWebNotificationShow (Required)-
closeclose function() => void (Required)-
ensurePermissionsrequest permissions function() => Promise<boolean | undefined> (Required)-
permissionGrantedpermission statusReact.MutableRefObject<boolean> (Required)-

UseWebNotificationShow

Returns

Notification | undefined

Arguments

ArgumentDescriptionTypeDefaultValue
titlenotification titlestring (Required)-
optionsoptions passed to NotificationOptionsNotificationOptions | undefined-