0

What I am trying to create is a function that simply sends notification to topic

So everything I do is just sending a channel name and what I have is this

exports.sendNotification = functions.https.onRequest((req, res) => {
 
  const dest = req.query.dest;
  
  console.log(dest);
  console.log('Message received');
    

    const payLoad = {
        notification:{
            title: "Message title",
            body: "You have a new message"
        }
    };

    console.log("*** about to send message ");
     admin.messaging().sendToTopic(dest, payLoad).then((response) => {
      console.log("Successfully sent message ", response);
      return res.send(JSON.stringify({"success":"true"}));
  })
  .catch((error) => {
      console.log("Error sending message: ", error);
      return res.send(JSON.stringify({"success":"false"}));
  })
    
});

From the functions logs I have:

11:12:47.622 PM sendNotification Function execution started

11:12:47.704 PM sendNotification 9Td4I6aWOoVvNXWNE0pZYjwbXNv1
11:12:47.705 PM sendNotification Message received
11:12:47.720 PM sendNotification Function execution took 98 ms, finished with status: 'crash'

It seems that it's crashing when the payLoad is created, but I can't figure out why

Thank you

Swift Sharp
  • 2,604
  • 3
  • 30
  • 54

1 Answers1

1

I am posting this as an answer, as it is too long for a the comment section.

Could you please try adding this line of code:

{
  cors(request, response, () => {

as suggested from this and this posts?

The poster from the first link seems to have received the exact same error with you and resolved it by enabling cors. You may find the official documentation here.