JavaScript Navigator setAppBadge() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
🔒 Secure context
Limited availability

What You’ll Learn

navigator.setAppBadge() sets a badge on your installed app icon — for example an unread count. Learn numeric badges, flag badges, clearing with 0 / clearAppBadge(), five examples, and try-it labs.

01

Kind

Method

02

Returns

Promise<undefined>

03

Status

Limited (not Baseline)

04

Context

Secure (HTTPS)

05

API family

Badging API

06

Pair with

clearAppBadge()

Introduction

Chat and mail apps show a small number on the home-screen icon when messages are unread. On the web, the Badging API lets an installed PWA do the same.

Call navigator.setAppBadge(contents) to set a count, or navigator.setAppBadge() with no argument for a simple “something is new” indicator (dot / flag, depending on the OS).

💡
No Dep / Exp / Non-standard banner

MDN marks Limited availability and a secure context, but not Deprecated, Experimental, or Non-standard. Always feature-detect and pair with clearAppBadge().

Understanding the setAppBadge() Method

  • Optional numbercontents becomes the badge value.
  • No argument — platform shows a flag/dot-style badge.
  • 0 clears — sets the badge to nothing (same idea as clear).
  • Promise — resolves with undefined on success.
  • Secure context — HTTPS / localhost.
  • Best on installed apps — dock / home-screen icons, not ordinary tabs.

📝 Syntax

General forms of the method:

JavaScript
navigator.setAppBadge()
navigator.setAppBadge(contents)

Parameters

  • contents (optional) — a number used as the badge value. If 0, the badge is cleared.

Return value

  • A Promise that fulfills with undefined.

Common exceptions / rejections

  • InvalidStateError — document not fully active.
  • SecurityError — blocked by same-origin policy in some cases.
  • NotAllowedError — permission not granted.

MDN-style unread count

JavaScript
const unread = 30;
navigator.setAppBadge(unread);

⚡ Quick Reference

GoalCode
Feature detecttypeof navigator.setAppBadge === "function"
Show countawait navigator.setAppBadge(5)
Show flag / dotawait navigator.setAppBadge()
Clear via setawait navigator.setAppBadge(0)
Clear via clearawait navigator.clearAppBadge()
Secure context?window.isSecureContext

🔍 At a Glance

Four facts to remember about navigator.setAppBadge().

Returns
Promise

undefined

Status
Limited

Not Baseline

Context
HTTPS

Secure required

Arg 0
clears

Badge nothing

📋 setAppBadge vs clearAppBadge

setAppBadgeclearAppBadge
PurposeSet count or flagRemove badge
ClearingsetAppBadge(0) also clearsDedicated clear call
Typical useUnread rose to 12User read everything
Beginner habitUpdate when count changesClear when count hits 0

Examples Gallery

Examples feature-detect first. On an ordinary browser tab you may get a resolved Promise with no visible badge — install as a PWA on a supporting OS to see the icon update.

📚 Getting Started

Detect Badging support and set a numeric badge.

Example 1 — Feature Detection

Check setAppBadge, clearAppBadge, and secure context together.

JavaScript
const lines = [
  "setAppBadge: " +
    (typeof navigator.setAppBadge === "function" ? "available" : "missing"),
  "clearAppBadge: " +
    (typeof navigator.clearAppBadge === "function" ? "available" : "missing"),
  "isSecureContext: " + window.isSecureContext
];
console.log(lines.join("\n"));
Try It Yourself

How It Works

If either method is missing, keep an in-page unread indicator instead.

Example 2 — Set an Unread Count (MDN)

Pass a number so the badge can display that value (for example 30).

JavaScript
async function setUnreadBadge(unread) {
  if (typeof navigator.setAppBadge !== "function") {
    return "Badging API not supported";
  }
  try {
    await navigator.setAppBadge(unread);
    return "Badge set to " + unread;
  } catch (err) {
    return "Error: " + err.name;
  }
}

setUnreadBadge(30).then(console.log);
Try It Yourself

How It Works

Update the badge whenever your unread count changes, not only on first load.

📈 Practical Patterns

Flag badges, clearing with zero, and a safe unread helper.

Example 3 — Flag Badge (No Contents)

Call with no argument for a non-numeric indicator.

JavaScript
async function setFlagBadge() {
  if (typeof navigator.setAppBadge !== "function") {
    return "API missing";
  }
  try {
    await navigator.setAppBadge();
    return "Flag / dot badge requested";
  } catch (err) {
    return "Error: " + err.name;
  }
}

setFlagBadge().then(console.log);
Try It Yourself

How It Works

Useful when you only need “there is something new,” not an exact count.

Example 4 — Clear With setAppBadge(0)

Passing 0 sets the badge to nothing.

JavaScript
async function clearWithZero() {
  if (typeof navigator.setAppBadge !== "function") {
    return "API missing";
  }
  await navigator.setAppBadge(3);
  await navigator.setAppBadge(0);
  return "Badge cleared via setAppBadge(0)";
}

clearWithZero().then(console.log).catch((err) => {
  console.log("Error: " + err.name);
});
Try It Yourself

How It Works

For readability, prefer clearAppBadge() when clearing is the only goal.

Example 5 — Safe Unread Helper

Set, update, or clear based on an unread count with structured errors.

JavaScript
async function syncAppBadge(unread) {
  if (typeof navigator.setAppBadge !== "function") {
    return { ok: false, reason: "unsupported" };
  }
  if (!window.isSecureContext) {
    return { ok: false, reason: "insecure-context" };
  }
  try {
    if (unread > 0) {
      await navigator.setAppBadge(unread);
      return { ok: true, reason: "set", unread: unread };
    }
    if (typeof navigator.clearAppBadge === "function") {
      await navigator.clearAppBadge();
    } else {
      await navigator.setAppBadge(0);
    }
    return { ok: true, reason: "cleared", unread: 0 };
  } catch (err) {
    return { ok: false, reason: err.name };
  }
}

syncAppBadge(7).then((r) => console.log(JSON.stringify(r)));
syncAppBadge(0).then((r) => console.log(JSON.stringify(r)));
Try It Yourself

How It Works

One helper keeps badge UI in sync with your message store without scattering API calls.

🚀 Common Use Cases

  • Unread messages — show how many chats need attention.
  • Task queues — badge pending review items for a work PWA.
  • Cart / wishlist — subtle flag when something was added offline.
  • Notification sync — update badge when push arrives while the app is closed.
  • Clear on read — remove the badge when the user opens the inbox.

🧠 How setAppBadge() Works

1

Decide the badge value

A count, a flag (no args), or 0 to clear.

Value
2

Call on HTTPS

Await navigator.setAppBadge(...) in a secure context.

Request
3

OS updates the icon

Installed PWA / dock / home-screen shows the badge.

Badge
4

Clear when caught up

Use clearAppBadge() or setAppBadge(0).

📝 Notes

  • Limited availability (not Baseline) — feature-detect always.
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Requires a secure context (HTTPS / localhost).
  • Visible badges are most reliable on installed PWAs.
  • Related: clearAppBadge(), share(), canShare(), sendBeacon().

Limited Browser Support

navigator.setAppBadge() belongs to the Badging API and is not Baseline. Support is strongest for installed PWAs on some Chromium-based platforms. Always feature-detect, use HTTPS, and keep an in-app unread UI as fallback.

Limited · Not Baseline

Navigator.setAppBadge()

Promise → set a numeric or flag badge on the app icon.

Limited Not Baseline
Google Chrome Badging for installed PWAs where supported
Limited
Microsoft Edge Follow Chromium Badging support
Limited
Opera Follow Chromium where available
Limited
Mozilla Firefox Typically unavailable — feature-detect
Unavailable
Apple Safari Check current PWA badging support
Limited
Internet Explorer No Badging API
Unavailable
setAppBadge() Limited

Bottom line: Detect the method, set counts on HTTPS for installed apps, clear with clearAppBadge() or setAppBadge(0), and never rely on a visible badge in every browser tab.

Conclusion

navigator.setAppBadge() is the Badging API way to put an unread count or flag on your installed app icon. Feature-detect it, call it on HTTPS, and clear with clearAppBadge() when the user is caught up.

Continue with share(), clearAppBadge(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Feature-detect before setting badges
  • Keep the badge equal to real unread state
  • Clear promptly when items are read
  • Test on an installed PWA
  • Keep an in-page unread counter too

❌ Don’t

  • Assume every desktop tab shows a badge
  • Leave stale counts after the inbox is empty
  • Spam huge numbers without meaning
  • Ignore NotAllowedError
  • Call only once and never update

Key Takeaways

Knowledge Unlocked

Five things to remember about setAppBadge()

Limited Badging API — set counts or flags on installed app icons.

5
Core concepts
📊 02

Support

Limited

Not Baseline
🔢 03

Number

shows count

contents
04

No args

flag / dot

Platform
05

Zero

clears badge

or clearAppBadge

❓ Frequently Asked Questions

It sets a badge on the app icon associated with the current site/PWA. Pass a number to show that count, omit the argument for a platform-defined flag/dot, or pass 0 to clear the badge.
No. MDN does not mark it Deprecated, Experimental, or Non-standard. It is Limited availability (not Baseline) and requires a secure context. No status banner is required.
Yes. The Badging API is a secure-context feature (HTTPS or localhost) in supporting browsers.
A Promise that resolves with undefined when the badge is set successfully.
Call navigator.clearAppBadge(), or navigator.setAppBadge(0). Prefer clearAppBadge() when your only intent is to remove the badge.
Often no. Badges usually appear on installed PWA / dock / home-screen icons. Desktop demos may resolve without a visible badge — that is expected for beginners.
Did you know?

Some platforms only show badges for apps the user installed to the home screen or dock. Your Promise can still resolve while a normal browser tab shows nothing — test with a real install when verifying UX.

Explore share() Next

Open the device’s native share sheet for URLs, text, and files.

share() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful