0

I'm having issues with the Firebase Functions. As soon as I execute firebase deploy in the terminal, I get the following message

=== Deploying to 'xxxxxxxx'... i deploying functions Running command: npm --prefix "$RESOURCE_DIR" run lint > functions@ lint D:\Flutter Projects\xxxxxxxx\xxxxxxxx\functions > eslint . + functions: Finished running predeploy script. i functions: ensuring required API cloudfunctions.googleapis.com is enabled... + functions: required API cloudfunctions.googleapis.com is enabled i functions: preparing functions directory for uploading... + Deploy complete! Project Console: https://console.firebase.google.com/project/xxxxxxxx/overview 

But when I take a look in the Firebase Functions console, it says "waiting for your first implementation".

This is my index.js file inside the functions folder:

const admin = require("firebase-admin"); var serviceAccount = require("../functions/xxxxxxxx-firebase-adminsdk.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://xxxxxxxx.firebaseio.com" }); var payload = { notification: { title: "Test title", body: "Test notification", image: "" }, }; var options = { priority: "high", to: '/topics/all' } function sendPushNotification(payload){ admin.messaging().sendToTopic("all", payload) .then((response) => { console.log("Successfully sent message: ", response); return null; }) .catch((error) => { console.log("Error sending message: ", error); }); } setInterval(() => { sendPushNotification(payload); }, 3600000); module.exports.sendPushNotification = sendPushNotification; 

When running node index.js the code works as expected. I'm not sure if I exported the function the right way, so maybe Firebase Functions doesn't know which function to deploy. The goal is that Firebase Functions sends a notification every hour (just for testing purposes) to all devices. How can I achieve this?

Note: I've checked the following (according to this thread):

  1. Actually saved the file with the function i'm trying to deploy.
  2. The file is in the correct directory. For JavaScript projects, the default is functions/index.js
  3. I am deploying the correct project from the correct directory.
0

1 Answer 1

1

You'll have to refactor your code to use a scheduled function.

const functions = require('firebase-functions'); const admin = require("firebase-admin"); const serviceAccount = require("../functions/xxxxxxxx-firebase-adminsdk.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://xxxxxxxx.firebaseio.com", }); const payload = { notification: { title: "Test title", body: "Test notification", image: "", }, }; const options = { priority: "high", to: '/topics/all' } exports.sendPushNotification = functions.pubsub.schedule('every 1 hour') .onRun((context) => { admin.messaging().sendToTopic("all", payload) .then((response) => { console.log("Successfully sent message: ", response); return null; }) .catch((error) => { console.log("Error sending message: ", error); return null; }); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.