【发布时间】:2018-04-22 12:33:57
【问题描述】:
如何将此有效负载对象传递给 Angular 4 组件?我读到了 postMessage 但它对我不起作用:(有人可以解释一下如何让它正常工作吗?
服务工作者:
messaging.setBackgroundMessageHandler((payload) => {
const notificationOptions = {
"body": data.body,
"icon": "icon.png",
"click_action": click_action
};
postMessage({'test'});
return self.registration.showNotification(notificationTitle, notificationOptions);
});
组件:
export class AppComponent implements OnInit {
constructor() {}
ngOnInit() {
navigator.serviceWorker.addEventListener('message', function(event) {
console.log("Got reply from service worker: " + event.data);
});
self.addEventListener('message', event => {
console.log('MESSAGE: ',event.data);
});
}
}
【问题讨论】:
标签: angular push-notification service-worker postmessage