How can i add simple link in html5 desktop notification - javascript

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: 'aaa'
});

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.

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

Related

I wish to create a website that gives alerts to drink water every n hours, how do I get an alert to show even if the user were to switch tabs?

following is the method that I tried to test, but the main problem I faced is that if the tabs are switched the alerts no longer appear and the queued alerts appear all together if the tab is reopened.
$(".button5sec").click(function()
{
setInterval(function(){alert("drink water bro!")}, 5000);
});
You potentially can use Desktop notification API.
https://developer.mozilla.org/en-US/docs/Web/API/notification
Something like this should work
function showNotification() {
const notification = new Notification("Drink water", {
body: "Don't forget to drink water"
})
}
if (Notification.permission === 'granted') {
} else if (Notification.permission !== 'denied') {
Notification.requestPermission();
}
$(".button5sec").click(function()
{
setInterval(function(){showNotification()}, 5000);
});

Showing notification only once after every refresh

what i wanted to do is only show the notification after the notification allowed.
what i have right now is, every after refresh on page it is always showing the notifications
this is my javascript
function notifyMe() {
function AutoRefresh( t ) {
setTimeout("location.reload(true);", t);
}
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
}
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
}
}
});
}
}
what should i do to make the notification only once, even after refresh.
You have to track if the user has already seen the notification and, if so, skip the rest of your notifyMe function. For example, you could store the information in localStorage.
function notifyMe() {
function AutoRefresh(t) {
// passing strings to setTimeout makes me shiver
// too many years of avoiding eval and stuff i guess :)
setTimeout(function () { location.reload(true); }, t);
}
if (localStorage.getItem('userHasSeenNotification')) {
return; // early return, exits the function
}
if (!('Notification' in window)) {
alert('Notifications ... ');
return; // early return, lets us get rid of the "else"
}
// Let's check whether notification permissions have already been granted
if (Notification.permission === "granted") {
// save seen state
localStorage.setItem('userHasSeenNotification', 1);
// If it's okay let's create a notification
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
};
return; // ... again ...
}
// Otherwise, we need to ask the user for permission
if (Notification.permission !== "denied") {
// save seen state
localStorage.setItem('userHasSeenNotification', 1);
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification('{!! $myname->body !!}');
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://localhost:8000/spektra/spektra-memberikan-aneka-tawaran-di-jakarta-fair-kemayoran-2018', '_blank');
};
}
});
}
}
}
Hint: You might want to move that last localStorage.setItem call into the callback passed to .then(function (permission) { ... }).

Desktop push notification?

I want to send the desktop notification to my customer dynamical from the server side. For example I have one website based on the e-commerce platform. I want to send the desktop notification to my customer what are the new offers are available. I created the notification with following code.. But I want to call the notification without any user activity.
$(window).load(function(){
if(!window.Notification){
console.log('sorry Notification not supported');
}
else {
Notification.requestPermission(function(p){
if(p === 'denied') {
} else if(p === 'granted') {
} else {
}
console.log(p);
});
}
var notify;
if(Notification.permission === 'default') {
console.log('Please allow Notification');
} else {
notify = new Notification('Cashback Notification..',{
body : 'Get assured cashback on purchases made via Getex.com',
icon : 'images/notification_getex.png'
});
}
});
Here i trigger notification only for the window load, but i want to notify from the server side like execute or call any API link.
Please help me Thanks!

Sending browser notification to specific users (web)

I would want to send out notifications to the list of users from database.
I found a solution to send push notifications:
// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "Hey there! You've been notified!",
});
notification.onclick = function () {
window.open("https://stackoverflow.com/a/13328397/1269037");
};
}
}
(Source: Chrome desktop notification example ) How can i configure it to send specific notifications to specific users ?
Let us assume that I want to send notifications to all users of the table user_notification.
I tried using
<?php
$sqx = "SELECT * FROM `user_js` ";
?>
in the code but it does not help either
step 1 : try adding the following into your HTML response :
<button onclick="notifyMe()">Notify me!</button>
step 2 : if you can see the button and you get a notification when you click on it, try to move on and trigger it as soon as the page loads.
see if this link will help you : http://www.w3schools.com/jsref/event_onload.asp
principally, this should work :
<body onload="notifyMe()">
step 3 : if all works well, you'll have to add a PHP condition that will add this "onload=..." only to the users you want.

How to get focus to a Chrome tab which created desktop notification?

I would like to implement same functionality as Gmail has nowadays. When new email arrives or new chat comes, notification popup appears and if you click it, the tab with Gmail gets focussed.
I have this code:
var n = window.webkitNotifications.createNotification('ico.gif', 'Title', 'Text');
n.onclick = function(x) { this.cancel(); };
n.show();
When I click notification it makes it just disappear. Now I need to add some code to onclick function to bring up and focus page that created this notification. I know it is possible because GMail does it very well. But I didn't succeed in looking into Gmail sources (they are minimalized and obfuscated).
Anybody knows how to do this ?
You can just place window.focus() in Google Chrome. It will focus to that window when clicked.
var n = window.webkitNotifications.createNotification('ico.gif', 'Title', 'Text');
n.onclick = function(x) { window.focus(); this.close(); };
n.show();
I opened the inspector in Gmail, added the above code, moved to a different tab, and ran it. The notification appeared and once clicked, it brought me back to Gmail.
Using Notifications.
if (typeof Notification !== 'undefined') {
alert('Please us a modern version of Chrome, Firefox, Opera or Safari.');
return;
}
Notification.requestPermission(function (permission) {
if (permission !== 'granted') return;
var notification = new Notification('Here is the title', {
icon: 'http://path.to/my/icon.png',
body: 'Some body text',
});
notification.onclick = function () {
window.focus();
};
});
window.focus() does not always work in recent Webkit browser versions (Chrome, Safari etc). But parent.focus() does.
Here's a complete jsfiddle: https://jsfiddle.net/wv0w7uj7/3/
Code:
function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "You've been notified!",
});
notification.onclick = function () {
parent.focus();
window.focus(); //just in case, older browsers
this.close();
};
}
}
It's not really good practice to use the onclick property, use the addEventListener for vanilla javascript or on method for jQuery.
var notify = new Notification('Test notification');
Vanilla:
notify.addEventListener('click', function(e) {
window.focus();
e.target.close();
}, false);
jQuery:
$(notify).on('click', function(e) {
window.focus();
e.target.close();
});
It should be this.close() rather than this.cancel(), like this:
var n = window.webkitNotifications.createNotification('ico.gif','Title', 'Text');
n.onclick = function(x) { window.focus(); this.cancel(); };
n.show();

Categories