Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add simple link in html5 desktop notification

How can i add simple link (a href) in html5 desktop notification on body section? I try onclick function, but it work's only few seconds. If i try press later the notification just disappear and nothing do. So the best way would be with link. I try that write, but then just print me as text.

var notification = new Notification('title', {
icon: '...',
body: '<a href="#">aaa</a>'
});
like image 932
Rob Avatar asked Dec 05 '14 21:12

Rob


People also ask

How do you put a notification in HTML?

Write a function showNotification(options) that creates a notification: <div class="notification"> with the given content. The notification should automatically disappear after 1.5 seconds. Use CSS positioning to show the element at given top/right coordinates.


1 Answers

Unfortunately there is no support for links and other markup in HTML notifications. The only method to get a clickable link with a notification is to use 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 has further documentation at https://developer.mozilla.org/en-US/docs/Web/API/notification

Firefox has a short duration for notifications compared to Chrome. There is no way to control how long a notification is visible in Firefox.

like image 90
JasonCG Avatar answered Oct 23 '22 10:10

JasonCG