Code
Browser Web APIs
Allows web applications to redirect audio output to a permitted Bluetooth headset, speakerphone, or other device.
Provides a way for browsers to better manage audio sessions and inform browsers about what kind of audio is being played.
Provides a method for managing downloads that may take a significant amount of time, running in the background if the user navigates away.
Enables web apps to defer tasks to be run in a service worker once the user has a stable network connection.
Allows scheduling of non-critical work to be performed during idle periods via requestIdleCallback.
Allows installed web apps to set a badge on the app icon to notify users of pending activity such as messages or updates.
Detects linear and two-dimensional barcodes, including QR codes, in images and video streams.
Provides information about the battery charge level of the device and notifications when the level or status changes.
Sends asynchronous, non-blocking requests to a server without expecting a response, ideal for analytics and diagnostics.
Enables simple communication between browsing contexts such as tabs, windows, and iframes sharing the same origin.
Badging API
Allows installed web applications to set a notification badge on the app icon in the operating system taskbar or dock. Useful for indicating unread messages, pending tasks, or other counters without opening the app. Requires the app to be installed as a PWA.
Key Methods
navigator.setAppBadge(count?)— Sets a badge. Pass a number for a count badge or omit for a plain dot badge.navigator.clearAppBadge()— Removes the badge from the app icon.
Example — Set a numeric badge
Show the number of unread messages on the app icon. Pass 0 or call clearAppBadge() to remove it.
async function updateUnreadCount(count) {
if ('setAppBadge' in navigator) {
if (count > 0) {
await navigator.setAppBadge(count);
} else {
await navigator.clearAppBadge();
}
}
}
// Show 3 unread messages
updateUnreadCount(3);
// Clear the badge
updateUnreadCount(0);Example — Set a flag badge (no count)
Call setAppBadge() without an argument to show a plain dot badge — useful for indicating activity without a specific number.
// Show a flag badge (dot) — no number
await navigator.setAppBadge();
// Later, clear it
await navigator.clearAppBadge();Example — Update badge from a service worker
Push notification handlers in service workers can update the badge to keep the icon count current even when the app is not open.
// service-worker.js
self.addEventListener('push', async (event) => {
const data = event.data?.json();
event.waitUntil(
Promise.all([
self.registration.showNotification(data.title, {
body: data.body,
}),
// Update badge with new unread count from push payload
self.navigator.setAppBadge(data.unreadCount),
])
);
});