Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

unable to catch any form of error or response from firebase notification callback function in Node js

I am using the package "fcm-node" in order to send notifications to certain device id.

the sendNotification function is as follows:

const FCM = require('fcm-node');
const serverKey = process.env.SERVER_KEY;
const fcm = new FCM(serverKey);

function sendNotification(registrationToken, title, body, type, key) {
    const message = {
        to: registrationToken,
        collapse_key: key,
        notification: {
            title: title,
            body: body,
            delivery_receipt_requested: true,
            sound: `ping.aiff`
        },
        data: {
            type: type,
            my_key: key,
        }
    };
    fcm.send(message, function (err, value) {
        if (err) {
            console.log(err);
            return false;
        } else {
            console.log(value);
            return value;
        }
    });
};

module.exports = {
    sendNotification
};

The api function I use to call this function is as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

router.get('/test', async (req, res, next) => {
  const promise = new Promise((resolve, reject) => {
    let data = sendNotification('', 'dfsa', 'asds', 'dfas', 'afsdf');
    console.log(data)
    if (data == false) reject(data);
    else resolve(data);
  });
  promise
    .then((data) => { return res.status(200).send(data); })
    .catch((data) => { return res.status(500).send(data) })
});

When I console.log the "err" and "value" from the sendNotification, I get either of the followings:

{"multicast_id":4488027446433525506,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1652082785265643%557c6f39557c6f39"}]};

{"multicast_id":8241007545302148303,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

In case it is successful, I made sure that the device is receiving the notification.

The problem is in the api’s data. It is always "undefined" and weither send notification is successful or not I get the 200 Ok status.

What seems to be the problem?

>Solution :

You can’t return anything from the function (err, value) {} callback of a node-style asynchrnous function.

Your sendNotification() function needs to return a promise. util.promisify() makes the conversion from a node-style asynchronous function to a promise-returning asynchronous function convenient. Note the return, it’s important:

const FCM = require('fcm-node');
const serverKey = process.env.SERVER_KEY;
const fcm = new FCM(serverKey);
const { promisify } = require('util');

fcm.sendAsync = promisify(fcm.send);

function sendNotification(registrationToken, title, body, type, key) {
    return fcm.sendAsync({
        to: registrationToken,
        collapse_key: key,
        notification: {
            title: title,
            body: body,
            delivery_receipt_requested: true,
            sound: `ping.aiff`
        },
        data: {
            type: type,
            my_key: key,
        }
    });
}

module.exports = {
    sendNotification
};

Now you can do what you had in mind

router.get('/test', async (req, res, next) => {
  try {
    const data = await sendNotification('', 'dfsa', 'asds', 'dfas', 'afsdf');
    return res.status(200).send(data);
  } catch (err) {
    return res.status(500).send(err);
  }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading