我有一个接收推送通知的服务人员,它在桌面版 Chrome 中运行良好,但在 Android 版 Chrome 中运行良好。
有时它按预期工作,但有时它开始打开 manifest.json 的 start_url 值而不是 notificationURL 变量的值。
通知 URL 如下所示: https ://www.example.com/abc/123/?source=notification
清单 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。
这与我已将网站添加到主屏幕这一事实有关吗?
这是预期的行为吗?
注意:它会打开清单 start_url,但不是在独立模式下。