--- url: "https://web.dev/articles/web-apps/badges" titulek: "How to create an app badge | Web Apps | web.dev" h1: "How to create an app badge Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/web-apps/badges" drobecky: ["Web Apps", "How to create an app badge"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 02:41:19" znaku: 8651 slov: 1236 kodovani: "iso-8859-2" sha256: "e093691a0354125d13b03407e1c8d882995af34a4b03641b5218a9ba84e01236" --- How to create an app badge Stay organized with collections Save and categorize content based on your preferences. Thomas Steiner Badges are used to convey non-urgent information to the user. For example, they are used to indicate the status of an app, or the number of unread items. The classic way of creating an app badge is to add a number to the favicon. On modern browsers, after an app has been installed, there is a built-in way to add a badge to the app icon in the operating system's task bar. The modern way Using the `navigator.setAppBadge()` method The `navigator.setAppBadge()` method sets a badge on the icon associated with the installed app. The method takes an optional single argument, which is an integer which will be used as the value of the badge. Setting the number to 0 clears the app badge. Not providing an argument results in a generic badge, commonly displayed as a colored dot. Browser Support 81 81 x 17 [Source](https://developer.mozilla.org/docs/Web/API/Navigator/setAppBadge) The classic way Adding a number to the favicon If the app is not installed yet, you can add a number to the favicon. There are many ways of doing so, for example, by drawing the favicon dynamically to a canvas with the badge info added and displaying it as a Blob URL, or to craft an SVG image with the badge info as a data URL. Progressive enhancement The snippet below uses a custom element [``](https://github.com/fallaciousreasoning/badgable-favicon#readme) that lets the developer declaratively set a badge on the favicon specified via the `src` attribute by passing an integer to the `badge` attribute. When the user installs the application, the badge is "upgraded" to a native operating system badge. `import 'https://unpkg.com/favicon-badge@2.0.0/dist/FavIconBadge.js'; // DOM references. const favicon = document.querySelector('favicon-badge'); const installButton = document.querySelector('button'); // Feature detection. const supportsAppBadge = 'setAppBadge' in navigator; let setAppBadge; // For the demo simply set the badge between [0, 9]. let i = 0; const getAppBadgeValue = () => { if (i > 9) { i = 0; } return i++; }; // Set the badge on the favicon. const setAppBadgeFavicon = (value) => { favicon.badge = value; }; // Set the native operating system badge. const setAppBadgeNative = (value) => { navigator.setAppBadge(value); } // If the app is installed and the Badging API is supported, // set the badge on the native operating system. Else, fall // back to the favicon. if ( matchMedia('(display-mode: standalone)').matches && supportsAppBadge ) { setAppBadge = setAppBadgeNative; } else { setAppBadge = setAppBadgeFavicon; } // Set a new badge every second. setInterval(() => { setAppBadge(getAppBadgeValue()); }, 1000); // If installation is supported… if ('BeforeInstallPromptEvent' in window) { let installEvent = null; const onInstall = () => { // After installation, "upgrade" to the native operating system badge. installButton.disabled = true; installEvent = null; if (supportsAppBadge) { favicon.badge = false; setAppBadge = setAppBadgeNative; } }; // …listen for the `beforeinstallprompt` event. window.addEventListener('beforeinstallprompt', (event) => { event.preventDefault(); installEvent = event; installButton.disabled = false; }); // Deal with installation. installButton.addEventListener('click', async () => { if (!installEvent) { return; } installEvent.prompt(); const result = await installEvent.userChoice; if (result.outcome === 'accepted') { onInstall(); } }); // Listen for the `appinstalled` in case the user installs the app manually. window.addEventListener('appinstalled', (event) => { onInstall(); }); }` Further reading [Badging for app icons](https://developer.chrome.com/docs/capabilities/web-apis/badging-api) Demo HTML ` How to create an app badge

How to create an app badge

  1. Watch the favicon. You should see a counter that updates each second integrated into the favicon. Favicon with counter.
  2. Install the app by clicking the button below. After the installation, the button is disabled.

  3. Watch the app icon in your operating system's task bar. You should see a counter that updates each second as an app badge. App badge with counter.
` CSS `html { box-sizing: border-box; font-family: system-ui, sans-serif; color-scheme: dark light; } *, *:before, *:after { box-sizing: inherit; } body { margin: 1rem; } img { height: auto; max-width: 100%; display: block; }` JS `import 'https://unpkg.com/favicon-badge@2.0.0/dist/FavIconBadge.js'; // The `` custom element. const favicon = document.querySelector('favicon-badge'); // The install button. const installButton = document.querySelector('button'); // Feature detection. const supportsAppBadge = 'setAppBadge' in navigator; // This function will either set the favicon or the native // app badge. The implementation is dynamically changed at runtime. let setAppBadge; // Variable for the counter. let i = 0; // Returns a value between 0 and 9. const getAppBadgeValue = () => { if (i > 9) { i = 0; } return i++; }; // Function to set a favicon badge. const setAppBadgeFavicon = (value) => { favicon.badge = value; }; // Function to set a native app badge. const setAppBadgeNative = (value) => { navigator.setAppBadge(value); } // If the app is installed and native app badges are supported, // use the native app badge. if ( matchMedia('(display-mode: standalone)').matches && supportsAppBadge ) { setAppBadge = setAppBadgeNative; // In all other cases (i.e., if the app is not installed or native // app badges are not supported), use the favicon badge. } else { setAppBadge = setAppBadgeFavicon; } // Update the badge every second. setInterval(() => { setAppBadge(getAppBadgeValue()); }, 1000); // Only relevant for browsers that support installation. if ('BeforeInstallPromptEvent' in window) { // Variable to stash the `BeforeInstallPromptEvent`. let installEvent = null; // Function that will be run when the app is installed. const onInstall = () => { // Disable the install button. installButton.disabled = true; // No longer needed. installEvent = null; if (supportsAppBadge) { // Remove the favicon badge. favicon.badge = false; // Switch the implementation so it uses the native // app badge. setAppBadge = setAppBadgeNative; } }; window.addEventListener('beforeinstallprompt', (event) => { // Do not show the install prompt quite yet. event.preventDefault(); // Stash the `BeforeInstallPromptEvent` for later. installEvent = event; // Enable the install button. installButton.disabled = false; }); installButton.addEventListener('click', async () => { // If there is no stashed `BeforeInstallPromptEvent`, return. if (!installEvent) { return; } // Use the stashed `BeforeInstallPromptEvent` to prompt the user. installEvent.prompt(); const result = await installEvent.userChoice; // If the user installs the app, run `onInstall()`. if (result.outcome === 'accepted') { onInstall(); } }); // The user can decide to ignore the install button // and just use the browser prompt directly. In this case // likewise run `onInstall()`. window.addEventListener('appinstalled', (event) => { onInstall(); }); }` Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-09-04 UTC.