0
votes

I am working on sending dynamic notification to user. Therefore, I am dealing with the Firebase Cloud Functions. Here is my question: How can I reach database in the cloud functions side?

Here is my codes:

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




// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

admin.initializeApp(functions.config().functions);


var newData;



//exports.myTrigger= functions.firestore.document().




exports.notificationTrigger = functions.firestore.document(`notifications/{notificationId}`).onCreate(async (snapshot, context) => {


    if (snapshot.empty) {
        console.log('No Devices');
        return;
    }
  var notificationData = snapshot.data();
  // notificationData.email;
  console.log(`Data  is : ${notificationData.email}`);


      admin.database().ref(`users/${notificationData.email}/deviceToken`).then((snapshot) => {
            var token = snapshot.data();
            console.log('token',token)
            return 1;
        }).catch(err => {
            console.error(err);

        }); 





    var tokens = [
        `${token.deviceToken}`
    ];



    var payload = {
        notification: {
            title: `Yeni Eklenen Değerin Maili : ${notificationData.email}`,
            body: 'hüsen',
            sound: 'default',
        },
        data: {
            click_action: 'FLUTTER_NOTIFICATION_CLICK',
            Messages:'Baba Mesajı from otomatik fonksiyon'
        },
    };

    try {
        const response = await admin.messaging().sendToDevice(tokens, payload);
        console.log('Notification sent successfully');
    } catch (err) {
        console.log(err);
    }
});  

As you can see, Firstly, I want to take email from nofications collection, then using that email, I want to search user which is using that email, After that, I want to take user deviceToken.

I tried so much things about syntax but I did not get any result from them :/

Likewise, I took some kind of errors:

Here you can see the errors:

  • Reference.child failed: First argument was an invalid path = "users/[email protected]/deviceToken". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]" at validatePathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1667:15) at validateRootPathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1679:5) at Reference.child (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:13843:17) at Database.ref (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:15097:48) at exports.notificationTrigger.functions.firestore.document.onCreate (/srv/index.js:37:24) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)
1

1 Answers

2
votes

The error message is telling you what went wrong:

Reference.child failed: First argument was an invalid path = "users/[email protected]/deviceToken". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

The path that includes the email address has an invalid character ..

If you want to store per-user data, you should instead use the user account's unique ID. Email addresses do not make very good unique identifier for a variety of reasons.