gpt4 book ai didi

node.js - 关闭通知的选项

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:15 25 4
gpt4 key购买 nike

我正在使用 HTML5 Notification API 创建一个 Electron 应用程序并希望每个通知在用户关闭通知时向用户显示几个选项,如下所示:

阅读 MDN documentation for a Notification constructor 后,我无法找到一种方法来配置通知,使其带有多个选项(上例中的“安装”和“稍后”选项)来关闭它。

options 对象中的 actions 属性(传递给 Notification 构造函数的第二个参数)似乎很有前途,但由于缺乏有关“NotificationAction”对象的文档,因此很难理解如何实现此功能。

来自 MDN 文档:

actions: An array of NotificationActions representing the actions available to the user when the notification is presented. These are options the user can choose among in order to act on the action within the context of the notification itself. The action's name is sent to the service worker notification handler to let it know the action was selected by the user.

目前,关闭通知时我只有一个选项(“关闭”)。

enter image description here

如有任何建议,我们将不胜感激。

最佳答案

在撰写本文时,通知操作尚未得到广泛支持。 Browser Compatability

对于支持它的浏览器,您可以像这样使用它:

self.registration.showNotification('New message from Alice', {
actions: [
{action: 'like', title: 'Like'},
{action: 'reply', title: 'Reply'}
]
});

然后获取用户点击的内容,如下所示:

self.addEventListener('notificationclick', function(event) {
var messageId = event.notification.data;

event.notification.close();

if (event.action === 'like') {
silentlyLikeItem();
}
else if (event.action === 'reply') {
clients.openWindow("/messages?reply=" + messageId);
}
else {
clients.openWindow("/messages?reply=" + messageId);
}
}, false);

您可以在https://developers.google.com/web/updates/2016/01/notification-actions阅读详细信息。

要查看此操作的实际效果,您可以查看 https://tests.peter.sh/notification-generator/#actions=3

关于node.js - 关闭通知的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com