Web Notification Message Notification

The Notification object is used to set and display desktop notifications for users, and its usage is relatively simple, directly instantiating the Notification object.

var notification  = new Notification(title,options);
  • Title: Display notification title
  • Options: Receive an object that is a configuration for displaying notifications

The configuration includes:

  • Dir: The direction of the text, with values of auto, ltr, and rtl. Default to auto ltr for left to right rtl for right to left
  • Lang: The language of the notification, this string must be valid in the BCP 47 language tag document;
  • Body: The content of the notification
  • Tag: The ID of the notification, which can be used to refresh, replace, or remove the notification
  • Icon: Icon address
  • Renotify: Boolean value indicating whether to replace the previous notification when a new one appears. Set to true to indicate replacement. If the tag is an empty string, it will throw a TypeError exception;

We can also obtain the user authorization status through Notification. permission, which is a read-only attribute;

  • Notification also provides monitoring of events:
  • Noshow/ondisplay: triggered when a notification is displayed;
  • Onclick: triggered when the user clicks on the notification
  • Oneerror: triggered when an error occurs in the notification
  • Onclose: triggered when the user closes the notification