ita
(Ita Gil)
November 23, 2023, 10:22pm
1
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.
ita
(Ita Gil)
November 24, 2023, 1:24pm
3
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);
Hello, were you able to solve this i am running into this issue as well
hi, i have the same error:
> error logs:
Error sending notification: FunctionError: TypeError: Value is not an object: undefined
how did you solve this?
hi i found this workaround, download firebase-admin version 10.0.0
npm install firebase-admin@^10.0.0
opened 11:10AM - 08 Jan 23 UTC
api: messaging
### [REQUIRED] Step 2: Describe your environment
* Operating System version… : MongoDB Atlas Trigger (Realm App)
* Firebase SDK version: 11.4.1
* Firebase Product: messaging
* JS SDK Version: [3.18.0](https://github.com/mongodb/stitch-js-sdk/releases/tag/v3.18.0)
* NPM version: I don't know
### [REQUIRED] Step 3: Describe the problem
Executing `admin.messaging().sendMulticast(message)` returns an error and is not working anymore after upgrading to `firebase-admin: 11.4.1` from `11.0.1` and with the release of [fastify/busboy 1.2.0](https://github.com/firebase/firebase-admin-node/issues/1902#issuecomment-1373978366) :
```
uncaught promise rejection: TypeError: Value is not an object: undefined
```
The difference with my previous issue #1902 is that now `messaging().sendMulticat(message)` is crashing and no longer deliver the push notification.
#### Steps to reproduce:
1. Create a MongoDB Atlas trigger
2. Add `firebase-admin` as external dependency and make sure it's `11.4.1`
3. Add the service-account.json as a `value`
4. Run the code with the below code
#### Relevant Code:
```
exports = async function() {
const admin = require("firebase-admin");
const json_creds = context.values.get("service_account_value_name");
const title = "Test";
const body = "This is a test";
const message= {
android: {
notification: {
title: title,
sound: "default",
body: body,
},
},
apns: {
payload: {
aps: {
alert: {
title: title,
body: body,
},
mutableContent: 1,
contentAvailable: 1
}
},
},
tokens: ["device_token_1", "device_token_2"]
};
admin.initializeApp({
credential: admin.credential.cert(JSON.parse(json_creds))
});
admin.messaging().sendMulticast(message);
console.log("Messages sent !");
}
}
```
Hey All, we had the same issue for sendMultiCast. Apparently Firebase deprecated in mid september the legacy support and is forcing the usage of new api which doesn’t support /batch that was used.
Installing the latest versions (12.4) does not work on the realm environment due to lack of full support for promises and the new jsonwebtokens npm package.
I created a customer version that works for the sendMultiCast (which can support sending ofc to a single token by providing an array with a single target token)