Javascript 在chrome for android中,推送通知在一段时间后不起作用
我的代码每5秒检查一次新消息。当新消息到达时,它会发出推送通知。然而,在我用android手机(我正在使用谷歌Chrome)访问页面几分钟后,这个推送功能才起作用。如果我访问该页面,10分钟后有人发送消息,在我再次手动进入该站点之前,我不会收到任何通知。你知道如何在一段时间后获得推送通知吗Javascript 在chrome for android中,推送通知在一段时间后不起作用,javascript,jquery,google-chrome,service-worker,web-push,Javascript,Jquery,Google Chrome,Service Worker,Web Push,我的代码每5秒检查一次新消息。当新消息到达时,它会发出推送通知。然而,在我用android手机(我正在使用谷歌Chrome)访问页面几分钟后,这个推送功能才起作用。如果我访问该页面,10分钟后有人发送消息,在我再次手动进入该站点之前,我不会收到任何通知。你知道如何在一段时间后获得推送通知吗 <script> function function_update(){ var text='hello world'; $.ajax({
<script>
function function_update(){
var text='hello world';
$.ajax({
type: "GET",
url: 'jquery_get_data.php',
data: { keyword : text },
success: function(data) {
/* alert(data);*/
if (data.trim()) {
$("#texterna").append(data);
window.scrollTo(0, 10000000);
////Notification start
navigator.serviceWorker.register('sw.js');
Notification.requestPermission(function(result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function(registration) {
registration.showNotification('New message', {
body: 'A new message has been made!',
icon: '/logos/ico_png.png',
badge: '/logos/ico_png.png',
vibrate: [200, 100, 200, 100, 200, 100, 200],
tag: 'vibration-sample',
target: 'https://www.example.com/'
});
});
}
});
////Notification ends
}
}
});
}
$(document).ready(function() {
setInterval("function_update()",5000); //call function every 5 seconds.
});
</script>
函数更新(){
var text='hello world';
$.ajax({
键入:“获取”,
url:'jquery\u get\u data.php',
数据:{关键字:文本},
成功:功能(数据){
/*警报(数据)*/
if(data.trim()){
$(“#texterna”)。追加(数据);
滚动到(0,10000000);
////通知开始
navigator.serviceWorker.register('sw.js');
Notification.requestPermission(函数(结果){
如果(结果==‘已授予’){
navigator.serviceWorker.ready.then(函数(注册){
registration.showNotification('新消息'{
正文:“已发出新消息!”,
图标:'/logos/ico_png.png',
徽章:'/logos/ico_png.png',
振动:[200100200100200200100200100200],
标签:“振动样品”,
目标:'https://www.example.com/'
});
});
}
});
////通知结束
}
}
});
}
$(文档).ready(函数(){
setInterval(“function_update()”,5000);//每5秒调用一次函数。
});
以下是服务人员文件“sw.js”的内容
self.addEventListener('notificationclick', function(event) {
let url = 'index.php';
event.notification.close(); // Android needs explicit close.
event.waitUntil(
clients.matchAll({type: 'window'}).then( windowClients => {
// Check if there is already a window/tab open with the target URL
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
// If so, just focus it.
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
// If not, then open the target URL in a new window/tab.
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});
self.addEventListener('notificationclick',函数(事件){
让url='index.php';
event.notification.close();//Android需要显式关闭。
event.waitill(
matchAll({type:'window'})。然后(windowClients=>{
//检查是否已打开带有目标URL的窗口/选项卡
对于(var i=0;i