我可以在哪里放置addEventListener(“ notificationclick”,()=> {})
我将以下代码放置在create-react-app中的serviceWorker.js中:
self.addEventListener("notificationclick", (event) => {
event.waitUntil(async function () {
const allClients = await clients.matchAll({
includeUncontrolled: true
});
let chatClient;
let appUrl = 'xyz';
for (const client of allClients) {
//here appUrl is the application url, we are checking it application tab is open
if(client['url'].indexOf(appUrl) >= 0)
{
client.focus();
chatClient = client;
break;
}
}
if (!chatClient) {
chatClient = await clients.openWindow(appUrl);
}
}());
});
上面的代码抛出未定义的错误客户端,并且当我单击通知时不会触发通知。