Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a link in the HTML5 notification?

I would like to be able to set a link/permalink to each notification, so when user clicks on it; then he is taken to the permalink location,

I've seen this answer which has a solution that's a little bit different because static link is used,

I would like to, somehow:

var noti = window.webkitNotifications.createNotification(
     'http://funcook.com/img/favicon.png', 
     'HTML5 Notification', 
     'HTML5 Notification content...',
     'http://mycustom.dynamic.link.com/'    /* invented code */
)
noti.onclose = function(){ alert(':(') };
noti.onclick = function(){ 
    window.location.href = $(this).url; /* invented code */
};
noti.show();

Any chance? (I really don't like the static html file solution... I would like to keep this syntax)

like image 596
Toni Michel Caubet Avatar asked Sep 02 '13 14:09

Toni Michel Caubet


2 Answers

How about something like this?

var createNotificationWithLink = function(image, title, content, link) {
    var notification = window.webkitNotifications.createNotification(image, title, content);

    notification.onclose = function() {
        alert(':(');
    };

    notification.onclick = function() { 
        window.location.href = link;
    };

    return notification;
};

Now you can call createNotificationWithLink whenever you want to create a notification:

var noti = createNotificationWithLink(
    'http://funcook.com/img/favicon.png',
    'HTML5 Notification',
    'HTML5 Notification content...',
    'http://mycustom.dynamic.link.com/'
);

noti.show();

You can also move noti.show(); into the createNotificationWithLink function if you like (notification.show();). I don't know if you want the notification to be shown automatically when you create it...

like image 143
Jasper N. Brouwer Avatar answered Sep 20 '22 00:09

Jasper N. Brouwer


You can pass a data property that you can read from within the onclick event handler as e.target.data.

var notification = new window.Notification("Hello!",
{
    body: "Hello world!",
    data: "https://www.example.com/?id=" + 123
});

notification.onclick = function(e) {
    window.location.href = e.target.data;
}
like image 43
Fred Avatar answered Sep 19 '22 00:09

Fred