notifications.onShown
当通知显示后立即触发。
语法
js
browser.notifications.onShown.addListener(listener)
browser.notifications.onShown.removeListener(listener)
browser.notifications.onShown.hasListener(listener)
事件有三个函数:
addListener(listener)
-
为此事件添加一个监听器。
removeListener(listener)
-
停止监听此事件。
listener
参数是要移除的监听器。 hasListener(listener)
-
检查是否已为此事件注册了
listener
。若正在监听,返回true
,否则返回false
。
addListener 语法
参数
listener
-
当此事件发生时调用的函数。该函数传入此参数:
notificationId
-
string
。已显示的通知的 ID。
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | ||||||
---|---|---|---|---|---|---|---|
onShown |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
示例
为 notifications.onShown
事件添加一个监听器并记录其详细信息:
js
function logShown(itemId) {
console.log(`消息已显示:${itemId}`);
browser.notifications.getAll().then((all) => {
console.log(all[itemId]);
});
}
browser.notifications.onShown.addListener(logShown);
备注:
该 API 基于 Chromium 的 chrome.notifications
API。