如何在 html5 桌面通知中添加简单链接

How can i add simple link in html5 desktop notification

本文关键字:添加 简单 链接 通知 桌面 html5      更新时间:2023-09-26

如何在正文部分的html5桌面通知中添加简单链接(href)?我尝试点击功能,但它只工作几秒钟。如果我稍后尝试按,通知就会消失,什么也没做。所以最好的方法是链接。我尝试写,但随后只是将我打印为文本。

var notification = new Notification('title', {
icon: '...',
body: '<a href="#">aaa</a>'
});
不幸的是,

HTML 通知中不支持链接和其他标记。 获取带有通知的可点击链接的唯一方法是使用 onclick:

function makeNotification() {
    var notification = new Notification('This is a clickable notification', {body: 'Click Me'});
    notification.onclick = function () {
      window.open("http://stackoverflow.com/");
    };
}
function notifyMe() {
    // Let's check if the browser supports notifications
    if (!("Notification" in window)) {
    alert("This browser does not support desktop notification");
    }
    // Let's check if the user is okay to get some notification
    else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    makeNotification();
    }
    // Otherwise, we need to ask the user for permission
    // Note, Chrome does not implement the permission static property
    // So we have to check for NOT 'denied' instead of 'default'
    else if (Notification.permission !== 'denied') {
        Notification.requestPermission(function (permission) {
          // If the user is okay, let's create a notification
          if (permission === "granted") {
            makeNotification();
          }
        });
    }
}

Mozilla 在 https://developer.mozilla.org/en-US/docs/Web/API/notification 有进一步的文档

与Chrome相比,Firefox的通知持续时间较短。 无法控制通知在 Firefox 中可见的时间长度。

在选项中添加 dir : "ltr"

    options = {
     dir : "ltr",
    icon: "images/images.jpg",
    body : "hello WOrld"
    }
new Notification("Current Notify",options);