我有一个服务工作线程可以接收推送通知,它在桌面版 Chrome 中运行良好,但在 Android 版 Chrome 中运行不佳。
有时它会按预期工作,但有时它会开始打开manifest.json的start_url值而不是notificationURL变量的值。
通知 URL 如下所示: https://www.example.com/abc/123/?source=notification
list start_url 值如下所示: https://www.example.com/?source=manifest
示例代码:
self.addEventListener('notificationclick', function(event) {
var notificationURL = event.notification.data.url;
event.notification.close();
event.waitUntil(clients.matchAll({
type: 'window'
}).then(function(clientList) {
for ( var i = 0; i < clientList.length; i++ ) {
var client = clientList[i];
if ( client.url === notificationURL && 'focus' in client ) {
return client.focus();
}
}
if ( 'openWindow' in clients ) {
// This prints "notificationURL: https://www.example.com/abc/123/?source=notification".
console.log('notificationURL:', notificationURL);
// But this opens "https://www.example.com/?source=manifest".
return clients.openWindow(notificationURL);
}
}));
});
因此,由于某种原因,在某些情况下,如果通知 URL 以“https://www.example.com ”开头,上面的代码将始终打开 https://www.example.com/?source=manifest .
这与我已将该网站添加到主屏幕有关吗?
这是预期的行为吗?
注意:它会打开 list start_url,但不会在独立模式下打开。
最佳答案
就在返回 client.focus();
添加此行
client.navigate(notificationURL);
此外,
更改返回clients.openWindow(notificationURL)
至
返回clients.openWindow(notificationURL).then(function(client){
客户端.navigate(notificationURL);
});
关于android - Service Worker client.openWindow 从 list 中打开 start_url 而不是请求的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37284495/