[英]Open installed PWA on notification click
I'm actually open a new window when user clicks a notification generated by the backend, this works fine on browser, because it open a new tab when the browser is opened or open a new window if not.当用户单击后端生成的通知时,我实际上打开了一个新的 window,这在浏览器上工作正常,因为它在打开浏览器时打开一个新选项卡,或者如果没有打开一个新的 window。
But if i have an installed PWA, on notification click it open a new browser window an not opens the PWA.但是,如果我安装了 PWA,在收到通知时单击它会打开一个新的浏览器 window,但不会打开 PWA。
here is my notificationclick event code:这是我的 notificationclick 事件代码:
if the PWA is open, i call focus() and then navigate(url), but if the PWA is closed i can't open the PWA and then call the focus and navigate functions.如果 PWA 是打开的,我会调用 focus(),然后再调用 navigation(url),但如果 PWA 关闭,我将无法打开 PWA,然后调用焦点和导航函数。
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();
}
}
}
}));
if there a way to open the PWA programatically?如果有办法以编程方式打开 PWA?
I was finally able to achieve what I wanted with the following:我终于能够通过以下方式实现我想要的:
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);
}
}
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.