简体   繁体   中英

push notification to specific device from another using firebase android java

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:

Firebase 部署后

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM