angular service-worker - 处理推送通知点击 [英] angular service-worker - handling push notification click
问题描述
如何处理 Angular Service Worker 中的推送通知点击.SWPush 没有任何方法.
How do I handle the push notification click in angular service worker. The SWPush does not have any methods.
我尝试引用一个 js,它监听notificationclick"事件处理程序,但它从未在点击通知按钮时被触发.
I have tried referencing a js which listens to the "notificationclick" event handler but it never got fired on the click of the notification button.
推荐答案
我建议将 angular 更新到至少 Angular 7.1.0 版本(我建议保持最新).有了这个,您现在可以订阅 notificationClicks
I suggest to update angular to at least Angular version 7.1.0 ( I would suggest to maintain up to date ). With this, you can now subscribe to notificationClicks
在构造函数中注入 SwPush
然后使用它:
In the constructor inject SwPush
then use it with:
this.swPush.notificationClicks.subscribe( arg =>
{
console.log(
'Action: ' + arg.action,
'Notification data: ' + arg.notification.data,
'Notification data.url: ' + arg.notification.data.url,
'Notification data.body: ' + arg.notification.body,
);
});
如果您不想更新,您应该使用 Webpack
或 gulp
等查看文件 ngsw-worker.js
和然后在行之后this.scope.addEventListener('push', (event) => this.onPush(event));
添加以下内容:
If you don't want to update you should watch the file ngsw-worker.js
with Webpack
or gulp
or etc. And then after the line with
this.scope.addEventListener('push', (event) => this.onPush(event));
add the following:
this.scope.addEventListener('notificationclick', (event) => {
console.log('Notification event', event);
event.notification.close();
var payload = event.notification.data;
if (event.action && payload.actions && payload.actions.includes(event.action)
clients.openWindow && event.notification.data.url) {
event.waitUntil(clients.openWindow(event.notification.data.url));
}
});
这篇关于angular service-worker - 处理推送通知点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!