0

I am trying to run the below cod which initialises the Firebase Admin SDK, and send a notification message.

const admin = require('firebase-admin/app');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');

const prop = PropertiesReader('properties.properties');

exports.sendSingleNotification = async (event, context) => {

    const params = event.queryStringParameters;
    var serviceAccount = require("xxx-xxx-firebase-adminsdk-xxx-xxx.json");

    try {
        admin.initializeApp({
            credential: admin.credential.cert(serviceAccount)
        });
        console.log("INITIALIZED");

        // This registration token comes from the client FCM SDKs.
        const registrationToken = params.fcmtoken;
        console.log()

        const message = {
            notification: {
              title: 'FooCorp up 1.43% on the day',
              body: 'FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
            },
            token: registrationToken
          };

        // Send a message to the device corresponding to the provided
        // registration token.
        admin.getMessaging().send(message)
            .then((response) => {
                // Response is a message ID string.
                console.log('Successfully sent message:', response);
                return {"response":response}
            })
            .catch((error) => {
                console.log('Error sending message:', error);
                return {"error 1":error}
            });
    } catch (error) {
        console.log(error);
        return {"error 2":error}
    }


};

Here the serviceAccount means the path of the Firebase private key file which is in the root of this project.

However when I run this code I always end up with the following error.

START RequestId: e66ffdd9-ab9c-4a68-ade2-7cfa97f42c31 Version: $LATEST
    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)] (/var/task/source/fcm/send-single-notification.js:14:42)rt' of undefined
END RequestId: e66ffdd9-ab9c-4a68-ade2-7cfa97f42c31

Something is undefined and I can't figure out what it is or what the error is.

How can I fix this?

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • Does this https://stackoverflow.com/questions/58974879/ solve your issue? Check the Nodejs part, it seems you are missing the database initialization. – Alex Jun 08 '22 at 20:18
  • @Alex I am not using database with this API. So that will not be a requirement. – PeakGen Jun 09 '22 at 03:39
  • Have you tried to change the security rules as suggested in the other answer? – Alex Jun 30 '22 at 22:46
  • You shouldn't use service accounts as files, use the secrets manager. Service account is very easy to peak steal and use for hackers. – Oliver Dixon Jul 05 '22 at 14:54

0 Answers0