Try to search your question here, if you can't find : Ask Any Question Now ?

Why firebase cloud function throwing error of 'The default Firebase app does not exist.'?

HomeCategory: stackoverflowWhy firebase cloud function throwing error of 'The default Firebase app does not exist.'?
Avatarcraig asked 1 week ago

So I am trying out firebase function for the first time. admin.messaging() throwing me the following error. Help me figure out why?
If I look at the console I get results till console.log('deviceToken', deviceToken);

so whats wrong in const messageDone = await admin.messaging().sendToDevice(deviceToken, payload);?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.updateUnreadCount = functions.database.ref('/chats/{chatId}/{messageId}')
  .onCreate(async(snap, context) => {
    const appOptions = JSON.parse(process.env.FIREBASE_CONFIG);
    appOptions.databaseAuthVariableOverride = context.auth;
    const adminApp = admin.initializeApp(appOptions, 'app');
    const { message, senderId, receiverUid } = snap.val();
    console.log(message, senderId, receiverUid);
    const deleteApp = () => adminApp.delete().catch(() => null);
    try {
      const db = adminApp.database();
      const reciverUserRef = await db.ref(`users/${receiverUid}/contacts/${senderId}/`);
      console.log('reciverUserRef', reciverUserRef);
      const deviceTokenSnapshot = await reciverUserRef.child('deviceToken').once('value');
      const deviceToken = await deviceTokenSnapshot.val();
      console.log('deviceToken', deviceToken);

      const payload = {
        notification: {
          title: 'Test Notification Title',
          body: message,
          sound: 'default',
          badge: '1'
      const messageDone = await admin.messaging().sendToDevice(deviceToken, payload);
      console.log('Successfully sent message: ', JSON.stringify(messageDone));
      return deleteApp().then(() => res);
    } catch (err) {
      console.log('error', err);
      return deleteApp().then(() => Promise.reject(err));

Update: According to this, admin.messaging().sendToDevice(deviceToken, payload) APIs are only available in the Admin Node.js SDK.
So switched to 

const payload = {
data: {
title: ‘Test Notification Title’,
body: message,
sound: ‘default’,
badge: ‘1’
token: deviceToken
const messageDone = await admin.messaging().send(payload);

Which not working either. Getting an error ` Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.`
1 Answers
Best Answer
AvatarArben answered 1 week ago
Your Answer

14 + 0 =

Popular Tags

WP Facebook Auto Publish Powered By :