gpt4 book ai didi

android - Service Worker clients.openWindow 从 list 中打开 start_url 而不是请求的 URL

转载 作者:搜寻专家 更新时间:2023-11-01 09:47:30 25 4
gpt4 key购买 nike

我有一个接收推送通知的服务 worker ,它在桌面版 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,但不是在独立模式下。

最佳答案

就在return client.focus();

之前

添加这一行

client.navigate(notificationURL);

此外,更改 return clients.openWindow(notificationURL)

返回 clients.openWindow(notificationURL).then(function(client){
client.navigate(notificationURL);
});

关于android - Service Worker clients.openWindow 从 list 中打开 start_url 而不是请求的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37284495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com