admin管理员组文章数量:1289893
Can I send browser notifications from server to subscribed users using PHP? I saw this tutorial today, but that work only from client side. I know there are services like pushcrew, but I want to develop my own using PHP and JavaScript.
My actual requirement is asking users to confirm if I can send them notifications using this code,
if (Notification.permission !== "granted")
{
Notification.requestPermission();
}
then show notifications when I publish new article in my website.
NB: Browser support doesn't matter.
Can I send browser notifications from server to subscribed users using PHP? I saw this tutorial today, but that work only from client side. I know there are services like pushcrew, but I want to develop my own using PHP and JavaScript.
My actual requirement is asking users to confirm if I can send them notifications using this code,
if (Notification.permission !== "granted")
{
Notification.requestPermission();
}
then show notifications when I publish new article in my website.
NB: Browser support doesn't matter.
Share Improve this question asked Feb 24, 2016 at 7:12 Joel JamesJoel James 1,3451 gold badge20 silver badges38 bronze badges 1- If you can get it to work, you could try this library: github./web-push-libs/web-push-php. I can't get it to work though, the PHP code inside the library is throwing errors even if I just use the example code on their website. – Donald Duck is with Ukraine Commented Dec 28, 2019 at 17:43
1 Answer
Reset to default 6You have to trigger these notifications on the client side, so you need to get them from the PHP server to your JavaScript:
- do an ajax polling every x seconds, for example with the jQuery ajax functions, and show a message if the server returned one
- push the message over WebSockets, for example with Ratchet
Web Push allows you to push notification even if the user didn’t have the site open and is supported by the recent Firefox 44. It is partially supported in Chrome. For details check out the Mozilla Hacks blog.
Example with polling
JavaScript jQuery part:
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./-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 server answer in "test.json" if there is a message:
{
"notify": true,
"title": "Hey there!",
"desc": "This is a new message for you",
"url": "http://stackoverflow."
}
JSON server answer in "test.json" to not show a message:
{
"notify": false
}
本文标签: javascriptBrowser notifications from server side using PHPStack Overflow
版权声明:本文标题:javascript - Browser notifications from server side using PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741461344a2380056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论