- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 angular 7 创建一个 PWA,但我正在努力处理推送通知。
我按照 this 的教程学习和 this使用 SWPush 和 Vapid key 对。我能够订阅和取消订阅,我的服务器部分也能正常工作,但不幸的是,教程不包括在应用程序中显示实际通知。
据我了解,通知应该会自动弹出,而无需在代码中调用它,对吗?。您只需订阅,ngsw 会完成剩下的工作。不幸的是,他们没有出现在我的案例中。我只能通过在代码中手动调用它们来显示它们,如下所示:
this.swPush.messages.subscribe(
(notification: any) => {
console.log("received push message", notification);
let options = {
body: notification.body,
icon: "assets/icon/favicon.png",
actions: <any>notification.actions,
data: notification.data,
vibrate: [100, 50, 10, 20, 20]
};
this.showNotification(notification.title, options)
},
err => {
console.error(err);
}
);
[...]
showNotification(title: string, options: NotificationOptions) {
navigator.serviceWorker.getRegistration().then(reg => {
reg.showNotification(title, options).then(res => {
console.log("showed notification", res)
}, err => {
console.error(err)
});
});
}
但这似乎只有当应用程序/页面在前台时才有效。否则我会收到类似“该网站已在后台更新”的通知。显然我在这里做错了什么。但是什么?
当应用程序处于后台时,我必须做什么才能显示通知?
有没有办法处理通知上的点击事件?
这里的 ng 文档真的很少。
最佳答案
经过大量的努力,我找到了一些东西并将其分享给其他人:
深入研究通过运行 build --prod 创建的 ngsw-worker.js 有这个函数:
onPush(msg) {
// Push notifications without data have no effect.
if (!msg.data) {
return;
}
// Handle the push and keep the SW alive until it's handled.
msg.waitUntil(this.handlePush(msg.data.json()));
}
...
handlePush(data) {
return __awaiter$5(this, void 0, void 0, function* () {
yield this.broadcast({
type: 'PUSH',
data,
});
if (!data.notification || !data.notification.title) {
return;
}
const desc = data.notification;
let options = {};
NOTIFICATION_OPTION_NAMES.filter(name => desc.hasOwnProperty(name))
.forEach(name => options[name] = desc[name]);
yield this.scope.registration.showNotification(desc['title'], options);
});
}
因为它看起来像 angular 订阅并为您显示通知,所以没有必要在您自己的代码中订阅和显示它。我的发送库 ( https://github.com/laravel-notification-channels/webpush ) 有问题,它发送的有效负载是这样的:
payload = {"title":"test","actions":[{"title":"View App","action":"view_app"}],"body":"test","icon":"\/icon.png","data":{"id":"21a3804e-6d71-4a56-b513-535709c37c0f"}}
但是 Angular 显然期望是这样的:
payload = {
"notification":{"title":"test","actions":[{"title":"View App","action":"view_app"}],"body":"test","icon":"\/icon.png","data":{"id":"21a3804e-6d71-4a56-b513-535709c37c0f"}}
}
导致 ngsw-worker.js 中的这一行失败:
if (!data.notification || !data.notification.title) {
return;
}
因此,没有显示任何通知,但浏览器在后台报告有更新。希望这对某人有帮助。
关于Angular 7 PWA 配置推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53363643/
我从上周开始就对 PWA 进行研究,我发现 Workbox 是一个很好的实现选择,我尝试使用 React+worbox+webpack 来实现 PWA 我能够创建应用程序图标,缓存 GET apis
我让 Chrome 在 Android 上安装了我的 PWA - 一旦安装,我想自动关闭安装它的浏览器窗口,并打开 PWA(因此用户不会在浏览器窗口中继续,认为他们正在使用PWA) - 这可能吗? 最
我使用 cra-template-pwa 模板创建了一个带有 create-react-app 的 PWA,但我无法根据更改更新应用程序。 现在内容被正确检测到,消息显示在控制台中: New cont
mailto 链接在以前版本的 iOS 上按预期工作。在使用 mailto 链接打开默认邮件应用程序时更新到 12.2+ 后,邮件应用程序可以正常工作。但是当回到 PWA 时,由于 iOS 功能更新到
我你去应用https://quotesaboutjesus.netlify.com/quotes并查看 Chrome 开发工具中的“应用程序”选项卡,几秒钟后您会看到服务工作线程运行良好...并且该应
我已经构建了一个带有角度前端的 ASP.NET Core 应用程序。角度应用程序具有 @angular/pwa节点包设置,因此它是一个渐进式 Web 应用程序,可以安装在 android/window
嘿寻找一些见解, 所以我以前从未使用过 Ionic,并试图给自己一个使用它的理由。但是,根据我一直在阅读的内容——根据项目的用例,一个可能比另一个更合适。我正在尝试构建一个不使用大量 native 功
有 3 种方法可以将 AMP 与 PWA 结合起来(如 here 中所述): 具有 PWA 功能的 AMP 页面 AMP 作为 PWA 的入口点 AMP 作为 PWA 的数据源 在第一种和第三种方法中
我正在结合企业应用服务器开发 PWA。 应用程序服务器为独立浏览器提供响应式 Web View ,并借助首次在 PWA 启动 url 上设置的 PWA cookie 自定义 PWA 上的 View 。
您好,我正在开发 Nextjs 项目,我尝试使用 next-pwa 将其转换为 PWA,首先我创建了 next.config.js const withPWA = require('next-pwa'
我正面临 Service Worker 的问题。 Service Worker 没有通过 app.module 自行注册。所以我在main.ts中手动注册它。 它在在线模式下工作正常 .但是当我将网络
我已将 PWA 功能和 service worker 添加到我的 angular 网站。发布到实时服务器后,许多用户开始遇到问题,例如:“无法访问此站点”,但是当用户单击 CTRL+ F5 时,它可以
我应该如何将分析集成到 PWA网络应用程序?如果我将来自 google 分析的分析添加到 webapp 中并将其发布在服务器中,那么稍后,如果我想在 Google Play 商店上发布它,我应该如何跟
当用户单击后端生成的通知时,我实际上打开了一个新窗口,这在浏览器上运行良好,因为它会在浏览器打开时打开一个新选项卡,否则打开一个新窗口。 但是如果我安装了 PWA,在收到通知时单击它会打开一个新的浏览
最近几天,我开始听说服务人员很酷。我想在我的 Angular 项目中实现服务人员,使我的 Web 应用程序完全离线工作,这意味着用户应该尽可能多地离线处理数据,一旦他们找到互联网,那么数据应该使用 M
我基于 current version 编写了一个简单的 PWA ( this tutorial)通过 Vaadin .它工作正常,在 Chrome 中测试过,也在离线模式下。 在移动设备上使用它会出
是否可以将图片直接从移动图库共享到 PWA(与我们发送图片时 What's app 或 Messenger 的行为相同),还是我需要一个 native 应用程序才能做到这一点?我正在构建的网站允许用户
下面是 our nuxt and nuxt-pwa的配置配置。 Nuxt pwa is recognising a new version available我们提示用户进行硬刷新/重新加载。 并且在
我的桌面通知在我的渐进式网络应用程序 (PWA) 中运行良好。打开 PWA 后,当我单击通知时,我能够接收通知的数据属性。当 PWA 关闭时,当我单击通知时,PWA 将打开,但我的 onclick 处
根据 PWA Web Bluetooth documentation ,它支持“BR/EDR 或 LE 连接”。 我正在尝试在 Chrome devtools 的控制台中使用以下代码进行测试: nav
我是一名优秀的程序员,十分优秀!