NotificationEvent:action 属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

注意:此功能仅在 Service Workers 中可用。

actionNotificationEvent 接口的一个只读属性,它返回用户点击的通知按钮的字符串 ID。如果用户点击了通知中除操作按钮以外的其他位置,或者通知没有按钮,则此值返回一个空字符串。通知 ID 在创建通知时通过 actions 数组属性设置,除非替换通知,否则无法修改。

字符串。

示例

js
self.registration.showNotification("New articles available", {
  actions: [{ action: "get", title: "Get now." }],
});

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

规范

规范
Notifications API
# dom-notification-actions

浏览器兼容性