Chrome desktop notification example
Initiate a Chrome desktop notification by first seeking user permission and later dispatching it. Here is a clear-cut approach:
You must call this within a user-generated event, like a click, because Chrome respects the user's peace.
Checking and requesting permission
Prior to sounding the alarm, remember to verify the user's permission status. Save the user's response using local storage to avoid pestering them with constant requests.
In scenarios where notifications can't live without user permission, use apt UX/UI to give them a soft push – describe how notifications could light up their world.
Using service workers
Service Workers are taking over desktop notifications in the web development realm. They work off-screen and are highly persistent, despite needing the same user consent as their desktop siblings.
To tap into your notifications’ potential with service workers, ensure the user’s browser is wearing the compatibility badge. Then, enroll a Service Worker:
Beyond this, Google's well-documented Web Fundamentals prepares you for a deep-dive into Service Worker notifications.
Managing notifications – Dos and don’ts
While deploying your notification brigade, keep the front-line browser compatibility in mind. Online platforms such as caniuse.com
are great, a crystal globe showing how different browsers react to your notification API.
Keep notifications rich and engaging with titles, bodies, unique icons, and user interaction handlers. For that push across multiple browsers, libraries like notify.js
can be your cavalry, introducing a host of fallbacks for the old guard.
The Notification API – Unlocking mysteries
Localize your permission request to avoid surprising the user with an unfamiliar language. This API is a living organism that is constantly evolving – as seen in Chrome 62, which disallows Notification requests from cross-origin iframes.
Let's manage the Notification API transition from the retired window.webkitNotifications
:
The latest Notification API standard is your regular go-to for recent changes.
Distribute far and wide – cross-browser support
When your notifications have to reach out to different browsers like Firefox or Opera, evolve your code to match them. The nifty notify.js library could also work on Internet Explorer and Microsoft Edge, although you might wish to put that to a test.
Was this article helpful?