1

Here I have code that trigger ref /tugas_course/{course_id}/{tugas_id} whenever child added will send notification to the android device. It already doing really well. But I want to add one more function inside it, I want to add child outside this ref called flag_tugas and will be populate with flag_tugas-course_id-tugas_id-user.uid: "o". I dont know how to add it, because the return value already take it all and how to get users id in cloud function.

export const onNotifTugas = functions.database.ref('/tugas_course/{course_id}/{tugas_id}') .onCreate((snapshot, context) =>{ const course_id = context.params.course_id; const tugas_id = context.params.tugas_id; console.log(`New Message ${course_id} in ${tugas_id}`); return admin.database().ref('/tugas/' + tugas_id +'/').once('value').then(snap => { const tugasData = snap.val(); const notifDataSend = { // buat structure data json dgn nama const notifDataSend untul cloud messaging data: { data_type: "tugas", title: "Anda mendapatkan notifikasi baru..", // data bebas (key, value) body: `Tugas ${tugasData.nama_tugas} baru`, // chatId = const chatId sound: "default" } }; console.log(`data yang dikirim `); return admin.messaging().sendToTopic(course_id, notifDataSend) .then(function(response) { console.log("Successfully sent message:", response); }) .catch(function(error) { console.log("Error sending message:", error); }); }).catch(error => { console.log(error); }) }); } 

Actually adding the new child under eclassapp enter image description here

Really thanks for your time and answer..

1 Answer 1

3

I am not sure to fully understand where you want to add the timestamp in your node tree (under /tugas_course/{course_id}/{tugas_id}/new_child or under /tugas_course/{course_id}/{tugas_id}) but the following code will work for adding a timestamp value right under /tugas_course/{course_id}/{tugas_id}. If you need you can change the value of ref to write the timestamp where you want.

exports.onNotifTugas = functions.database .ref('/tugas_course/{course_id}/{tugas_id}') .onCreate((snapshot, context) => { const ref = snapshot.ref; const ts = admin.database.ServerValue.TIMESTAMP; return ref.update({ date_time: ts }); }); 

Note however that you don't need a Cloud Function to add a timestamp from the server, see https://firebase.google.com/docs/reference/js/firebase.database.ServerValue

So in your case you would do something like:

var tugaRef = firebase.database().ref("/tugas_course/course_id/tugas_id"); tugaRef.push({ foo: bar, //Other data of your 'tugas' object createdAt: firebase.database.ServerValue.TIMESTAMP }) 
Sign up to request clarification or add additional context in comments.

3 Comments

I just want to when I create child in that ref, so it will add one more child which is time that post being created. For specific information, I want to child called "date_time" and contains value like "Tue Apr 23 16:08:28 GMT+05:30 2013"
Just adapt the ref value in the answer to fulfill your needs. I've renamed createdAt field to date_time. The timestamp value correspond to the format you are looking for.
Sir, now I realize I have to add child like this. I have been updated my code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.