Javascript 如何使用Angular 7&;FCM(Firebase云消息)
我目前正在根据firebase-messaging-sw.js和messaging.service.ts接收到的推送通知,实现一个通知计数器。 我在messaging.service.ts(前台通知)中计算了通知数,并使用Behavior Subject将此通知的值发送到我的主要组件,在这里我可以显示这些新通知的数量(当我们在应用程序中时收到推送通知),但我现在正在努力如何发送(自动通知)firebase-messaging-sw.js(js文件)中计算的后台通知对我的主要组件(TypeScript文件)的值 firebase-messaging-sw.jsJavascript 如何使用Angular 7&;FCM(Firebase云消息),javascript,angular,typescript,firebase-cloud-messaging,Javascript,Angular,Typescript,Firebase Cloud Messaging,我目前正在根据firebase-messaging-sw.js和messaging.service.ts接收到的推送通知,实现一个通知计数器。 我在messaging.service.ts(前台通知)中计算了通知数,并使用Behavior Subject将此通知的值发送到我的主要组件,在这里我可以显示这些新通知的数量(当我们在应用程序中时收到推送通知),但我现在正在努力如何发送(自动通知)firebase-messaging-sw.js(js文件)中计算的后台通知对我的主要组件(TypeScri
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/5.5.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.5.0/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
messagingSenderId: 'MY-SENDER-ID'
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
let amountOfNotifications = 0;
messaging.setBackgroundMessageHandler(function(payload) {
if (payload.data.notification_type === 'message_001') {
amountOfNotifications = amountOfNotifications + 1;
console.log('amount of background notifications', amountOfNotifications);
// how to emit amoutOfNotifications to another typescript component???
}
// here you can override some options describing what's in the message;
// however, the actual content will come from the Webtask
const notificationOptions = {
body: 'New activity'
};
return self.registration.showNotification('Example App', notificationOptions);
});