Home > Enterprise >  unable to catch any form of error or response from firebase notification callback function in Node j
unable to catch any form of error or response from firebase notification callback function in Node j

Time:05-09

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:

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:1652082785265643U7c6f39557c6f39"}]};

{"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?

CodePudding user response:

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);
  }
});

CodePudding user response:

Maybe it will help, at first try to return your response (the promise) in sendNotification, as actually you have a void function, that's why it's always undefined and after in your route

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