I’m trying to send a push notification to an iOS client from an Atlas Function but I get error Error sending notification: FunctionError: TypeError: Value is not an object: undefined on line await admin.messaging().send(message);
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);
}
}
Any idea why this is happening?
Tech stack:
- SwiftUI app with Realm.
- Firebase Messaging on the client code.
firebase-admindependency on Atlas Function.
I tried:
- Printing
admin,admin.messaging(), andadmin.messaging.send- they’re all fine (not undefined). - Printing
message,message.notification,message.token- also fine (not undefined). - Changing the message’s notification object name to “data” - didn’t help.