我们在发送通知云功能实时数据库时如何获取用户UID?

时间:2018-10-08 04:35:21

标签: android firebase firebase-realtime-database google-cloud-functions

我具有在onchildadded时触发并在android中向订阅用户发送通知的功能。但是我有用户UID将其存储到新引用。其实我想向用户发送有通知的UID,这可以吗?

export const onNotifTugas = functions.database.ref('/tugas_course/{course_id}/{tugas_id}')
.onCreate((snapshot, context) =>{


   //////here i want to get the uid and store it//////

    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);
    })

});

0 个答案:

没有答案