How to include a link in the HTML5 notification?
Solution 1
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...
Solution 2
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;
}
Solution 3
noti.onclick = function() {
window.open("http://www.stackoverflow.com")
};
More on how to use window.open: http://www.w3schools.com/jsref/met_win_open.asp4
HTML5 Notification Resource: http://www.html5rocks.com/en/tutorials/notifications/quick/ (If this doesn't answer you're question)
For Each one you could have:
var noti1 = window.webkitNotifications.createNotification(
'http://funcook.com/img/favicon.png',
'HTML5 Notification',
'HTML5 Notification content...',
var noti2 = window.webkitNotifications.createNotification(
'http://funcook.com/img/favicon.png',
'HTML5 Notification #2',
'HTML5 Notification #2 content...',
etc
and then
noti1.onclick = function() {
window.open("http://www.stackoverflow.com")
};
noti2.onclick = function() {
window.open("http://www.example.com")
};
hope that helps :)
Related videos on Youtube
Toni Michel Caubet
Front end developer and wordpress lover; based in Mallorca, Spain. Founder @ Funcook
Updated on June 29, 2022Comments
-
Toni Michel Caubet almost 2 years
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)
-
Toni Michel Caubet over 10 yearsNot really... because the plan is to each notification to have its own url.. so the url needs to be attached to the notification
-
Toni Michel Caubet over 10 yearsThank your for your answer, unfortunatelly this stills too static; imagine I am delivering one notification per post and per user... how can I track the links?
-
Toni Michel Caubet over 5 yearswhy not
var new_window = window.open(event.target.data, '_blank');
?