【发布时间】:2019-02-22 06:07:53
【问题描述】:
我想发送触发到 pengumuman 主题的通知。
export const onNotifPengumuman = functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')
.onCreate((snapshot,context) =>{
const course_id_p = context.params.course_id_p;
const pengumuman_id = context.params.pengumuman_id;
const nama_matkul = admin.database().ref('/courses/'+course_id_p+'name').once('value').then(snap =>{
return snapshot.val();
}).catch(error =>
{
console.log(error);
})
console.log(`cobacobacoba ${nama_matkul}`);
return admin.database().ref('pengumuman/' + pengumuman_id + '/').once('value').then(snap =>{
const pengumumanData = snap.val();
const notifDataPengumuman = {
data:{
data_type: "pengumuman ",
title: "Pengumuman Baru", // data bebas (key, value)
body: `${nama_matkul}`, // chatId = const chatId
sound: "default"
}
}
return admin.messaging().sendToTopic(course_id_p, notifDataPengumuman)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}).catch(error => {
console.log(error);
})
});
在第一个参考functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')我想在firebase实时数据库中访问和触发这个孩子,下面的代码:
在此代码return admin.database().ref('pengumuman/' + pengumuman_id + '/') 之后,我试图获取有关 pengumuman 的所有信息并将其发送给用户。下面的代码:
enter image description here
但在此之前,我想在数据库中的课程引用中获取 pengumuman 名称以获取名称的值,使用以下代码:
const nama_matkul = admin.database().ref('/courses/'+course_id_p+'name').once('value').then(snap =>{
return snapshot.val();
}).catch(error =>
{
console.log(error);
})
问题是当我使用该代码获取子名称并将其存储到 matkul 中时,当我发送/记录时,它将返回承诺对象。我想要显示“REKAYASA PERANGKAT LUNAK”的结果。 谢谢,抱歉解释不好
[已修复]
我正在尝试解决方案并找到此代码
export const onNotifPengumuman = functions.database.ref('/pengumuman_course/{course_id_p}/{pengumuman_id}')
.onCreate((snapshot,context) =>{
const course_id_p = context.params.course_id_p;
console.log(`course id pengumuman ${course_id_p}`);
const pengumuman_id = context.params.pengumuman_id;
admin.database().ref('/courses/' + course_id_p + '/').once('value').then(snap2 =>{
const nama_matkul = snap2.child('name').val();
console.log(`nama matkul dari sini ${nama_matkul}`);
admin.database().ref('pengumuman/' + pengumuman_id + '/').once('value').then(snap =>{
const pengumumanData = snap.val();
const notifDataPengumuman = {
data:{
data_type: "pengumuman",
title: "Pengumuman Baru", // data bebas (key, value)
body: `Judul :${nama_matkul}`, // chatId = const chatId
sound: "default"
}
}
return admin.messaging().sendToTopic(course_id_p, notifDataPengumuman)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}).catch(error => {
console.log(error);
})
}).catch(error =>{
console.log(error);
})
});
【问题讨论】:
-
试试
ref('/courses/{courseId}/name')
标签: node.js firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions