- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用服务人员来处理后台通知。当我收到一条消息时,我正在创建一个新的 Notification
使用 self.registration.showNotification(title, { icon, body })
.我正在使用 self.addEventListener('notificationclick', ()=>{})
查看通知上的点击事件.单击时,我正在检查是否有 WindowClient
已打开,如果已打开,我将获取其中一个窗口客户端并调用 postMessage
在它上面将通知中的数据发送到应用程序以允许应用程序处理通知。万一没有打开的窗口我调用openWindow
一旦完成,我将使用 postMessage
将数据发送到该窗口.
event.waitUntil(
clients.matchAll({ type: 'window' }).then((windows) => {
if (windows.length > 0) {
const window = windows[0];
window.postMessage(_data);
window.focus();
return;
}
return clients.openWindow(this.origin).then((window) => {
window.postMessage(_data);
return;
});
})
);
postMessage
调用
openWindow
永远不会交付。我猜这是因为
postMessage
调用
WindowClient
在页面完成加载之前发生,所以 eventListener 还没有注册来监听该消息?是对的吗?
最佳答案
我也偶然发现了这个问题,使用超时是反模式,也可能导致延迟大于可能失败的 10 秒 chrome 限制。
我所做的是检查是否需要打开一个新的客户端窗口。
如果我在 clients 数组中没有找到任何匹配项 - 这是瓶颈,您需要等到页面加载完毕,这可能需要一些时间,并且 postMessage 将无法正常工作。
对于这种情况,我在服务 worker 中创建了一个简单的全局对象,该对象在特定情况下被填充,例如:
const messages = {};
....
// we need to open new window
messages[randomId] = pushData.message; // save the message from the push notification
await clients.openWindow(urlToOpen + '#push=' + randomId);
....
在加载的页面中,在我的 React 应用程序中,我等待我的组件已安装,然后我运行一个函数来检查 URL 是否包含“#push=XXX”哈希,提取随机 ID,然后发回消息给服务人员向我们发送消息。
...
if (self.location.hash.contains('#push=')) {
if ('serviceWorker' in navigator && 'Notification' in window && Notification.permission === 'granted') {
const randomId = self.locaiton.hash.split('=')[1];
const swInstance = await navigator.serviceWorker.ready;
if (swInstance) {
swInstance.active.postMessage({type: 'getPushMessage', id: randomId});
}
// TODO: change URL to be without the `#push=` hash ..
}
最后在 service worker 中我们添加一个消息事件监听器:
self.addEventListener('message', function handler(event) {
if (event.data.type === 'getPushMessage') {
if (event.data.id && messages[event.data.id]) {
// FINALLY post message will work since page is loaded
event.source.postMessage({
type: 'clipboard',
msg: messages[event.data.id],
});
delete messages[event.data.id];
}
}
});
messages
我们的“全局”不是持久的,这很好,因为当推送通知到达时服务 worker “唤醒”时我们只需要它。
关于firebase-cloud-messaging - Service Worker - 在 postMessage 之前等待 clients.openWindow 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49954977/
我有一个带有表格和“打印”按钮的文档。打印按钮调用 javascript 函数以在新窗口中生成可打印版本。可打印版本应该从站点加载样式表。但是样式表不会加载。当我从新打开的窗口打开源代码时,虽然样式表
我有一个按钮需要在新选项卡中打开共享文件位置才能下载它。文件位置的形式为:\\myserver\myfolder\file.doc\ 但是,当我使用时 window.open("\\myserver\
我正在尝试运行以下 JavaScript 程序, function myFunction() { var x; if (confirm("Press a button!") == true)
我有一个由以下文本打开的 html 文件: onClick="OpenWindow('Images/SupplierHelp.html', 'Large Business Concern',600,
本文整理了Java中com.haulmont.cuba.gui.WindowManager.openWindow()方法的一些代码示例,展示了WindowManager.openWindow()的具体
我使用以下代码在弹出窗口上加载 WebContent 编辑 portlet: " /> editUrl 的值为: editUrl = "javascript:Liferay.Util.openWind
我的 WorkbenchAdvisor 子类的 openWindows() 覆盖中有以下代码: public class MyWorkbenchAdvisor extends WorkbenchAdv
我们有一个 selenium IDE 脚本,正在转换为 python webdriver。以下命令未转换: [openWindow | http://mywebsite.com/index.php |
我正在使用服务人员来处理后台通知。当我收到一条消息时,我正在创建一个新的 Notification使用 self.registration.showNotification(title, { icon
我有一个接收推送通知的服务 worker ,它在桌面版 Chrome 中运行良好,但在 Android 版 Chrome 中运行不佳。 有时它会按预期工作,但有时它会开始打开 manifest.j
我正在 Chrome 版本 42.0.2311.152m 下进行测试,我想实现在通知点击上打开一个窗口,如下例所示:(来源:https://developer.mozilla.org/en-US/do
我是一名优秀的程序员,十分优秀!