Atlas Function with firebase-admin dependency failing to send push notification

Hi there,

I have a SwiftUI app with Realm. I set up Firebase Messaging on the client code.

I’m trying to send a push notification to an iOS client from an Atlas Function.

I added ‘firebase-admin’ dependency.

Here’s the function:

async function sendPushNotification(token, title, body) {
  const admin = require('firebase-admin');
  
  const credentials = {
    projectId: '<project id>',
    clientEmail: '<client email>',
    privateKey: '<private key>',
  };

  admin.initializeApp({
   credential: admin.credential.cert(credentials)
 });
 
 const message = {
    notification:{
      body: body,
      title: title
    },
    token: token
  };

  try {
    await admin.messaging().send(message);
    console.log('Notification sent successfully');
  } catch (error) {
    console.error('Error sending notification:', error);
  }
}

I get the error: Error sending notification: FunctionError: TypeError: Value is not an object: undefined

Any ideas why this is happening?

I printed admin, admin.messaging(), and admin.messaging.send - they’re all fine (not undefined).
I also printed message, message.notification, message.token - also fine (not undefined).
And I tried to change the message’s notification object name to “data” - didn’t help.

Provide full error log which shows line numbers etc.

The only log printed (via Atlas Logs) is

Error:
Error sending notification: FunctionError: TypeError: Value is not an object: undefined

I’m using Atlas Function Editor, and I can tell based on debugging that the line where it fails is
await admin.messaging().send(message);