0

I need your help with my code, I need to send a notification when a user does action from his app to notify another user(which I have his device token).

What I already did: first, install node.js and from it download firebase admin SDK and login and connect it to my project on firebase until I got Firebase CLI connected successfully.

then I opened the index.js file and add this code to it:

const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.pushNotification = functions.database.ref('/YourNode/{pushId}').onWrite((change, context) => {
    console.log('Push notification event triggered');

    //  Get the current value of what was written to the Realtime Database.
    const valueObject = change.after.val(); 

    // Create a notification
    const payload = {
        notification: {
            title: valueObject.title,
            body: valueObject.message,
            sound: "default"
        }
    };

    //Create an options object that contains the time to live for the notification and the priority
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
    };

    return admin.messaging().sendToTopic("pushNotifications", payload, options);
});
 

and then I return to cmd screen and enter firebase deploy but at this point, it returns an error which is:

after firebase deploy

and when I google this problem I figured that I need to pay for a membership to firebase, right?

What I need

1- If that's right is my code and my steps right? and what should I do next to get the notifications working?

2- Can you help to give me the right and specific steps to accomplish that?

Please don't give me any link to firebase, I read All of the documentation and really confused.

Thanks for your time, waiting for your help :)

4
  • Just to clarify: the exact case is that you need to send a push message to somebody else upon some request to your node.js app? And where do you host your node.js app? Commented Oct 21, 2020 at 22:04
  • 2
    @mrpasqal From the code shared and the tagging that seems to be Firebase's Cloud Functions. Commented Oct 22, 2020 at 2:18
  • Answer on the one question I could is below. If the questions #1 and #2 still apply with that answer, please edit your question to show what the problem is when you run this code. Commented Oct 22, 2020 at 2:21
  • @mrpasqal I am trying to use Firebase cloud function to send that push notifications in case the other user app sleep Commented Oct 22, 2020 at 10:39

1 Answer 1

1

I figured that I need to pay for a membership to firebase, right?

If your code requires Node.js 10 or higher, it can no longer be deployed on Firebase's free plan, and you'll indeed need to be on the pay-as-you-go plan. The error message you get also explicitly mentions this. I recommend studying the Firebase pricing FAQ on Cloud Functions, as it's quite complicated and I don't want to misquote that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply, I need to be sure I am doing the right steps to get the notification? If so l will pay for a plan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.