![](/img/trans.png)
[英]How to receive data, sent from the browser using XMLHttpRequest, in server-side (php)?
[英]Browser notifications from server side using PHP
您必須在客戶端觸發這些通知,因此您需要將它們從PHP服務器發送到JavaScript:
Web Push允許您即使用戶沒有打開網站並且受最新的Firefox 44支持,也可以推送通知。Chrome對此提供了部分支持。 有關詳細信息,請查看Mozilla Hacks博客 。
輪詢示例
JavaScript jQuery部分:
function doPoll() {
// Get the JSON
$.ajax({ url: 'test.json', success: function(data){
if(data.notify) {
// Yeah, there is a new notification! Show it to the user
var notification = new Notification(data.title, {
icon:'https://lh3.googleusercontent.com/-aCFiK4baXX4/VjmGJojsQ_I/AAAAAAAANJg/h-sLVX1M5zA/s48-Ic42/eggsmall.png',
body: data.desc,
});
notification.onclick = function () {
window.open(data.url);
};
}
// Retry after a second
setTimeout(doPoll,1000);
}, dataType: "json"});
}
if (Notification.permission !== "granted")
{
// Request permission to send browser notifications
Notification.requestPermission().then(function(result) {
if (result === 'default') {
// Permission granted
doPoll();
}
});
} else {
doPoll();
}
如果有消息,JSON服務器將在“ test.json”中回答:
{
"notify": true,
"title": "Hey there!",
"desc": "This is a new message for you",
"url": "http://stackoverflow.com"
}
“ test.json”中的JSON服務器答案不顯示消息:
{
"notify": false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.