I have an app that is running on firebase BaaS. I need to send notifications to a specific user when an action occur and at the same time retrive the notification to display it.
I've followed some guide and the documentations to implement it in my service worker and I was able to get a notification logged in console with this code when using the firebase console.
importScripts('https://www.gstatic.com/firebasejs/9.1.3/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.1.3/firebase-messaging-compat.js');
firebase.initializeApp({
apiKey: "...",
authDomain: "...firebaseapp.com",
databaseURL: "https://...-default-rtdb.firebaseio.com",
projectId: "...",
storageBucket: "...appspot.com",
messagingSenderId: "...",
appId: "...",
measurementId: "..."
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.onBackgroundMessage( (payload) => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here for 'data' key
let { data } = payload
if( data && data.title && data.body ) {
const notificationTitle = data.title || 'Test title';
const notificationOptions = {
body: data.body || 'Test body',
};
self.registration.showNotification(notificationTitle, notificationOptions);
}
});
self.addEventListener('notificationclick', (event) => {
let { notification } = event;
// this event will not be occoured by notication of FCM 'notification' key
console.log('[firebase-messaging-sw.js] Notification clicked: ', event);
notification.close()
});
Since the final user will not have access to the firebase FCM console, how I can add the ability to send a notification from my vue app, and how I can retrive it when a notification is recived?