NotificationEvent:action 属性

Limited availability

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

备注: 此特性仅在 Service Worker 中可用。

NotificationEvent 接口的 action 只读属性返回用户单击的通知按钮的字符串 ID。如果用户单击通知除操作按钮以外的区域,或者通知没有按钮,则此值返回空字符串。通知 ID 在创建通知期间通过 actions 数组属性设置,并且无法修改(除非通知被替换)。

一个字符串。

示例

js
self.registration.showNotification("新的可用文章", {
  actions: [{ action: "get", title: "立即获取" }],
});

self.addEventListener(
  "notificationclick",
  (event) => {
    event.notification.close();
    if (event.action === "get") {
      synchronizeReader();
    } else {
      clients.openWindow("/reader");
    }
  },
  false,
);

规范

Specification
Notifications API
# dom-notification-actions

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
action

Legend

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

Full support
Full support
No support
No support