Skip to main content

Firebase Notification

Here is an example of notification with Firebase realtime database. :::

Installation in API

npm i flamingo-firebase-api-function

Instructions to use

First import the package

const firebaseObject = require('flamingo-firebase-api-function');

Update value in firebase database

const privateKey = JSON.parse(process.env.CONFIG_PRIVATE_KEY);
const firebaseData = {
config: {
type: process.env.CONFIG_TYPE,
project_id: process.env.CONFIG_PROJECT_ID,
private_key_id: process.env.CONFIG_PRIVATE_KEY_ID,
private_key: privateKey.private_key,
client_email: process.env.CONFIG_CLIENT_EMAIL,
client_id: process.env.CONFIG_CLIENT_ID,
auth_uri: process.env.CONFIG_AUTH_URI,
token_uri: process.env.CONFIG_TOKEN_URI,
auth_provider_x509_cert_url: process.env.CONFIG_AUTH_PROVIDER,
client_x509_cert_url: process.env.CONFIG_CLIENT_CERT_URL,
},
inputData: {
count: notificationCount,
},
userId: result.user_id,
databaseURL: process.env.DATABASEURL,
collectionURL: process.env.COLLECTIONURL,
};
await firebaseObject.firebaseDataUpdate(firebaseData);

Parameters need to pass

const firebaseData = {
config: {
type: '',
project_id: '',
private_key_id: '',
private_key: '',
client_email: '',
client_id: '',
auth_uri: '',
token_uri: '',
auth_provider_x509_cert_url: '',
client_x509_cert_url: '',
}, // Find these keys from firebase
inputData: {
count: notificationCount, // Unread notification count
},
userId: '', // Onject id of the user in users collection
databaseURL: '', // Firebase database URL
collectionURL: '', // eg: notifications
};

This will trigger a function in the package and retrives the values in firebase database

exports.firebaseDataUpdate = (data) => {
return new Promise((resolve, reject) => {

if (!firebase.apps.length) {
firebase.initializeApp({
credential: firebase.credential.cert(data.config),
databaseURL: data.databaseURL
})
}

const database = firebase.database();
const ref = database.ref(`${data.collectionURL}/${data.userId}`);

resolve(ref.set(data.inputData));
})
}

This will update the firebase data.

Installation in Frontend

npm i flamingo-firebase-frontend-function

Instructions to use (Frontend)

First import the package

import { firebaseDataFetch } from 'flamingo-firebase-frontend-function';

This is how we receive data from firebase

const firebaseData = {
config: envValues.FIREBASE_CONFIG,
userId: getCookies('USERID'),
collectionURL: envValues.COLLECTIONURL,
};

firebaseDataFetch(firebaseData).then((unreadNotification) => {
setNotificationCount(unreadNotification.count); // Set the notification count to a variable
});

Parameters need to pass

const firebaseData = {
config: {}, // Config values
userId: getCookies('USERID'), // User id in mongo collection
collectionURL: envValues.COLLECTIONURL, // eg: notifications
};


config : {
apiKey: '',
authDomain: '',
databaseURL: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: '',
measurementId: '',
} // get these values from firebase

This will trigger a function in the package and retrives the values in firebase database

const firebaseDataFetch = (data) => {
return new Promise((resolve, reject) => {
const firebase = initializeApp(data.config);
const database = getDatabase(firebase);
onValue(ref(database, `${data.collectionURL}/${data.userId}`), (snapshot) => {
const unreadCount = snapshot.val();
resolve(unreadCount);
});
});
};