我正在为一家餐馆制作一个网络应用程序,并且已经到了我的通知让我知道何时有新订单的地步。该应用程序的工作流程是:
1) 服务员通过Android Chrome登录查看未决票
2) 客户端使用另一台设备注册票
3) 服务员设备上显示通知
到目前为止,一切正常。我想知道的是:有没有什么方法可以在使用其他应用程序(或查看主屏幕时)时点击通知在相应的选项卡(服务员登录的位置)上打开 Chrome?
这是我的代码:
App.js
var n = _registration.showNotification('Menú',{
body: 'Tienes un nuevo pedido',
requireInteration: true,
});
服务 worker
self.addEventListener('notificationclick', function(event) {
let url = 'https://example.com/some-path/';
event.preventDefault();
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);
}
})
);
});
这在桌面浏览器中运行得非常好,但由于服务员使用的是 Android 手机,因此这还不够。谢谢。
最佳答案
对我来说,client.focus()
也不起作用,但是当我点击通知时 — 它实际上聚焦了我的应用程序。
我的代码的不同之处在于我没有 event.preventDefault();
关于Javascript - 在 Android 设备上单击服务工作人员通知后打开浏览器选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52470749/