Chrome 扩展通知不显示 contextMessage?

Chrome Extension notifications not showing contextMessage?

在我的代码中,contextMessage 没有显示 我知道它与 chrome.notifications.create 一起工作正常,但如果我使用它,那么我就无法做到这一点,因此通知永远不会消失,除非被关闭或点击

function Notify(Title, Body, miniMsg, Url, Icon) {
    var notification = new Notification(Title, {
        icon: Icon||"Logo.png",
        body: Body,
        contextMessage:miniMsg
    });
    notification.onclick = function () {
        window.open(Url);
    }
}

谢谢!

答案很简单:兼容性。

根据 MDN. For a Google Chrome extension, it is suggested to use the chrome.notifications API.

,您正在尝试使用不带 webkit 前缀的 Chrome 中不支持的功能

使用 chrome.notifications API,您无法指定通知保持打开的时间。但是,另一种选择是使用 chrome.experimental.notifications API,这会阻止您将扩展发布到网上商店,并且文档很少。

您还可以将 chrome.notifications.create 中的 priority 选项更改为 -2 到 2 之间的任何整数,其中 2 是最高的,并且会授予您的通知最多显示时间。

由于您似乎在使用 Notifications 对象,您可能会注意到您的语法与 MDN 文档中显示的语法不匹配。首先,通知似乎没有任何 contextMessage 参数。作为旁注,onclick 回调将不起作用,因为 Url 参数不再在当前范围内执行。匿名函数将无法看到传递给 NotifyUrl 参数。您可能还想查看 compatibility and notes for Notifications.