Firebase Function always finished with timeout

i am using firebase function for creating notifications with cloud messaging. But i am always getting this Error:

Function execution took 60006 ms, finished with status: 'timeout'

but the notification works.

This is the code i am using in index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();


exports.SendNotification = functions.https.onRequest((req, res) => {  

  var payload = {
      notification: {
         title: "this is a test",
         body: req.rawBody.toString('utf8')
      }
  }

  return admin.messaging().sendToTopic("all", payload);

});

Do i have to implement a response? When, how do i do that?

J3nsis

Answers:

Answer

A HTTPS-triggered Cloud Function ends when it sends a response to its caller. Since your code never sends a response, the code keep running until its configured timeout (which is 1 minute by default).

To properly terminate the function when it's done, send a result back after the FCM call completes:

admin.messaging().sendToTopic("all", payload).then(() => {
  res.status(200).send("ok");
}).catch((err) => {
  res.status(500).send(err);
});

I recommend reading this section in the docs:

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.