Navigator:setAppBadge() 方法

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

Navigator 接口的 setAppBadge() 方法在与此应用关联的图标上设置徽章。如果将值传递给该方法,则该值将被设置为徽章的值。否则,徽章将显示为点或平台定义的其他指示器。

语法

js
setAppBadge()
setAppBadge(contents)

参数

contents 可选

一个将用作徽章值的数值。如果 contents0,则徽章将设置为 nothing,表示已清除徽章。

返回值

一个兑现 undefinedPromise

异常

InvalidStateError DOMException

如果文档未完全激活,则抛出该异常。

SecurityError DOMException

如果调用被同源策略阻止,则抛出该异常。

NotAllowedError DOMException

如果 PermissionStatus.state 不是 granted,则抛出该异常。

示例

在下面的示例中,未读的计数被传递给 setAppBadge()。然后徽章应显示 30

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

规范

Specification
Badging API
# setappbadge-method

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
setAppBadge

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

参见