【发布时间】:2019-04-21 03:56:27
【问题描述】:
我正在使用 Angular 7 创建 PWA,但我正在努力处理推送通知。
我使用 SWPush 和 Vapid 密钥对以 this 和 this 的身份关注教程。我能够订阅和取消订阅,而且我的服务器部分也能正常工作,但不幸的是,教程并未涵盖在应用程序中显示实际通知。
据我了解,通知应该会自动弹出,而无需在代码中调用它,对吧?您只需订阅,其余的由 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 文档真的很少。
【问题讨论】:
-
您还有一份文档要查看,您是否尝试过this documentation 的网络基础知识?
-
谢谢,是的,我已经看到了,最后这就是我上面的代码正在做的事情。但是,当应用程序处于后台时,它似乎不起作用:-/
标签: angular push-notification service-worker progressive-web-apps web-push