我正在 Chrome 版本 42.0.2311.152m 下进行测试,我想实现在通知点击上打开一个窗口,如下例所示:(来源:https ://developer.mozilla.org/en-US/docs/Web/API /窗口客户端 )
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});
我的文件结构是这样的:
https://myurl.no-ip.org/app/index.html
https://myurl.no-ip.org/app/manifest.json
https://myurl.no-ip.org /app/service-worker.js
我有一个问题,我总是得到一个
无效访问错误
在 service-worker.js 中调用 clients.openWindow('/') 或 clients.openWindow(' https://myurl.no-ip.org/app/index.html ') 时,我收到错误消息:
{code: 15,
message: "Not allowed to open a window.",
name: "InvalidAccessError"}
永远不会到达“return client.focus()”行,因为 client.url 永远不会只是'/'。看着
clients.matchAll({type: "window"})
.then(function (clientList) {
console.log(clientList[0])});
我看到了我当前的 WindowClient:
{focused: false,
frameType: "top-level",
url: "https://myurl.no-ip.org/app/index.html",
visibilityState: "hidden" }
'focused' 和 'visibilityState' 属性是正确的并且可以正确更改。
通过进行手动焦点调用
clients.matchAll({type: "window"})
.then(function (clientList) {
clientList[0].focus()});
我收到错误:
{code: 15,
message: "Not allowed to focus a window.",
name: "InvalidAccessError"}
我认为问题在于 url 不仅仅是'/'。你对此有什么想法吗?
非常感谢!
最好的问候
安迪