服务工作者:当他点击通知时将用户重定向到不同的 url [英] Service worker: Redirecting a user to different url when he clicks the notification
本文介绍了服务工作者:当他点击通知时将用户重定向到不同的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码段有助于使应用标签成为焦点,或使用该网址打开新标签.如果选项卡已经打开,是否可以更改 URL(根据用户点击的通知)?
This snippet helps in either bring in the app tab in focus or open a new tab with that URL. If the tab is already open, is there a way to change the URL (based on the notification the user clicked).?
event.waitUntil(
clients.matchAll({
type: 'window'
})
.then(function(windowClients) {
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
if (url.test(client.url) && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow('/#/chat');
}
})
);
推荐答案
WindowClient.navigate()
方法听起来像你想要的.您应该能够执行以下操作:
The WindowClient.navigate()
method sounds like what you want. You should be able to do something like:
// Assume that you want to find the first window that is currently open
// to originalUrl, and navigate that window to navigationUrl.
// If there is no window currently at originalUrl, then open a new window.
event.waitUntil(
clients.matchAll({type: 'window'})
.then(clients => clients.filter(client => client.url === originalUrl))
.then(matchingClients => {
if (matchingClients[0]) {
return matchingClients[0].navigate(navigationUrl)
.then(client => client.focus());
}
return clients.openWindow(navigationUrl);
})
);
这篇关于服务工作者:当他点击通知时将用户重定向到不同的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文