Notification API with Page Visibility API
When writing with Javascipt for the web, there are many APIs available for us to use. Some are very mature such as DocumentFragment while some are more experimental (yet still usable) like WebGL. In this article we combine two of these APIs to make a feature that many websites implement, sending messages to the user using the native browser notification (not to be confused with push notifications).
Notification API
The image above is a simple example of a native browser notification. In the example, we use the title
, body
, and icon
properties.
Properties:
- title is the string ‘Hi i\’m Chris!’ (this is required)
- body is the string ‘Notification API with Page Visibility API’
- icon is the url string for the icon
Create Notification
const title = 'Hi i\'m Chris!';
const body = 'Notification API with Page Visibility API';
const icon = '/icon.ico';
const options = {body, icon};
...