2016-08-23 11 views
0

ブラウザが閉じているとき、またはブラウザのタブが閉じているときにWeb通知を取得する必要があります。ブラウザまたはタブが閉じられたときにWeb通知を取得する方法

マイコードは以下のとおりです。ユーザーがタブを閉じたり、クリックされたときを検出するためにonbeforeunloadまたはonunloadを聞くことができ、

<!DOCTYPE html> 
    <html> 
    <head> 
    <title>Display browser Notification from Web Application Demo</title> 
    <script type="text/javascript"> 

var articles = [ 
["HOW TO GET RS 2000 LENSKART GIFT VOUCHER FOR 890 USING NEARBUY LENSKART OFFER.","http://www.findyoursolution.in/get-rs-2000-lenskart-gift-voucher-890-using-nearbuy-lenskart-offer/"], 
["HOW TO GET RS 100 CASHBACK ON RS 500 RECHARGE AND BILLS PAYMENT AT PAYZAPP APP.","http://www.findyoursolution.in/get-rs-100-cashback-rs-500-recharge-bills-payment-payzapp-app/"], 
["HOW TO GET 500MB 3G-2G DATA FOR FREE BY USING AIRCEL FREE INTERNET TRICK","http://www.findyoursolution.in/get-500mb-3g2g-data-free-using-aircel-free-internet-trick/"], 
["HOW TO AVAIL NEARBUY PVR CINEMA OFFER","http://www.findyoursolution.in/avail-nearbuy-pvr-cinema-offer/"], 
["GIVE STRATEGIC IMPORTANCE TO INDIAN TELECOM SECTOR: DELOITTE HASKINS SELLS ON BUDGET EXPECTATIONS.","http://www.findyoursolution.in/give-strategic-importance-to-indian-telecom-sector-deloitte-haskins-sells-on-budget-expectations/"], 
["AIRTEL TRICK UNLIMITED GUJRAT 3G 4G UDP TRICK","http://www.findyoursolution.in/airtel-trick-unlimited-gujrat-3g-4g-udp-trick-4920012-port/"], 
["HOW TO DOWNLOAD MYVODAFONE APP AND GET 100MB 3G DATA.","http://www.findyoursolution.in/download-myvodafone-app-get-100mb-3g-data/"], 
["HOW TO GET RS 20 FREE RECHARGE FROM AGROSTAR APP.","http://www.findyoursolution.in/get-rs-20-free-recharge-agrostar-app/"], 
["WHAT ARE THE NAMES OF SNOW WHITE SEVEN DWARFS.","http://www.findyoursolution.in/names-snow-whites-seven-dwarfs/"] 
];  

setTimeout(function(){ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
}, 200000);  

document.addEventListener('DOMContentLoaded', function() 
{ 

if (Notification.permission !== "granted") 
{ 
Notification.requestPermission(); 
} 

document.querySelector("#notificationButton").addEventListener("click", function(e) 
{ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
e.preventDefault(); 
}); 

//=================================== 
setInterval(function(e){ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
e.preventDefault(); 

}, 5000); 
//=================================== 
}); 

function notifyBrowser(title,desc,url) 
{ 
if (!Notification) { 
console.log('Desktop notifications not available in your browser..'); 
return; 
} 
if (Notification.permission !== "granted") 
{ 
Notification.requestPermission(); 
} 
else { 
var notification = new Notification(title, { 
icon:'http://i2.wp.com/www.findyoursolution.in/wp-content/uploads/2016/06/cropped-Logo-512.jpg?fit=180%2C180', 
body: desc, 
}); 

// Remove the notification from Notification Center when clicked. 
notification.onclick = function() { 
window.open(url);  
}; 

// Callback function when the notification is closed. 
notification.onclose = function() { 
console.log('Notification closed'); 
};  
} 
} 

</script>  

<style type="text/css"> 
.hover{background-color: #cc0000} 
#container{ margin:0px auto; width: 800px} 
.button { 
font-weight: bold; 
    padding: 7px 9px; 
    background-color: #5fcf80; 
    color: #fff !important; 
    font-size: 12px; 
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; 
    cursor: pointer; 
    text-decoration: none; 
    text-shadow: 0 1px 0px rgba(0,0,0,0.15); 
    border-width: 1px 1px 3px !important; 
    border-style: solid; 
    border-color: #3ac162; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    display: -moz-inline-stack; 
    display: inline-block; 
    vertical-align: middle; 
    zoom: 1; 
    border-radius: 3px; 
    box-sizing: border-box; 
    box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset; 
} 
.authorBlock{border-top:1px solid #cc0000;} 
</style> 
</head> 
<body> 
<div id="container"> 
<h1>Display browser Notification from Web Application Demo</h1> 

<h4>Click notification button</h4> 
<a href="#" id="notificationButton" class="button">Notification</a> 
</div> 

</body> 
</html> 
+0

について(エッジがあなたの関数が返すメッセージが表示されます、それは文字列でなければなりません)のFirefoxやChrome

を独自のメッセージを持つことはできません、あなたはより多くを説明してくださいもらえますか?あなたは彼が閉じるときにユーザーに通知しますか(または閉じるボタンのボタンを押して)? – M98

+0

はい私はブラウザを閉じている場合は通知を来る必要がありますが、上記のコードはブラウズ閉じる時に通知されません。 – Jitesh

+1

Onbeforeunloadはあなたが探しているイベントです - たぶん - あなたのコードがしなければならないことが分かりませんあなたの質問で –

答えて

0

私が正しくあなたを理解している場合:サイトが開いているときに通知が動作しますが、ブラウザやタブが閉じられていないときon go backボタン。ここで

それを行うには、いくつかの方法があり、単純なexample

であるように、あなたは、HTML属性としてonbeforeunloadを設定することができます注

<body onbeforeunload="return showAlert()"> 

:showAlertは()という関数であります文字列は文字列を返し、文字列は一部のブラウザのメッセージになります(Microsoft Edgeはカスタムメッセージをサポートしていません)

または、addE ventListener。しかし、あなたは

+0

ブラウザが閉じているときに通知が表示されません。ブラウザまたはタブが閉じるときに通知を受け取る方法あなたは私のコードを実行することができますし、チェックしてください – Jitesh

+0

彼は通知が 'onbeforeunload'ではないことを期待しています@Kermani – Karthi

+0

@ KarthiVenture Right、しかし彼は言った:**閉じるブラウザまたはClose Tab **を持っているときにWeb通知を取得する方法、 * browser close event *へのアクセスは 'onbeforeunload'または' onunload'を使用しています。 – M98

関連する問題