实际上,当用户单击后端生成的通知时,我实际上是在打开一个新窗口,这在浏览器上效果很好,因为它在打开浏览器时会打开一个新标签,否则会打开一个新窗口。
但是,如果我已经安装了PWA,则在通知时单击它会打开一个新的浏览器窗口,而不会打开PWA。
这是我的notificationclick事件代码:
如果PWA已打开,则先调用focus(),然后再导航(url),但是如果PWA关闭,则无法打开PWA,然后再调用focus和Navigation函数。
event.waitUntil(clients.matchAll({
type: "window"
}).then(function (clientList) {
console.log('clientList: ', clientList);
if (data.WebUrl) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
console.log('client: ', JSON.stringify(client), client);
if ('focus' in client) {
client.focus();
// try to navigate to the url in the focused client
client.navigate(data.WebUrl).then((response) => {
event.notification.close();
});
break;
}
else {
clients.openWindow(data.WebUrl);
event.notification.close();
}
}
}
}));
是否可以通过编程方式打开PWA?
答案 0 :(得分:0)
我终于可以通过以下方法实现我想要的目标:
event.waitUntil(clients.matchAll({
type: "window",
includeUncontrolled: true
}).then(function (clientList) {
if (data.WebUrl) {
let client = null;
for (let i = 0; i < clientList.length; i++) {
let item = clientList[i];
if (item.url) {
client = item;
break;
}
}
if (client && 'navigate' in client) {
client.focus();
event.notification.close();
return client.navigate(data.WebUrl);
}
else {
event.notification.close();
// if client doesn't have navigate function, try to open a new browser window
return clients.openWindow(data.WebUrl);
}
}
}));