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

标签 android google-chrome service-worker web-push

我有一个服务工作线程可以接收推送通知,它在桌面版 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/

相关文章:

html - href mailto 和 iOS safari

service-worker - 我的 PushRegistration 对象上不存在 keys 属性

Android 计费异常

c# - Xamarin EditText InputType 密码

android - 没有主键的sqlite?

android - Room - 字符串类型转换器数组

javascript - 为什么 toString() 和 this.toString() 在 Chrome 的控制台中产生不同的结果?

Java:通过进程控制浏览器

javascript - 将 firebase-messaging-sw.js deps 与 Webpack 捆绑在一起

html - 在 ServiceWorker 中访问 indexedDB。竞争条件