Javascript Firebase函数日志为空
因此,当一个值被添加到数据库中的通知时,我试图从firebase运行以下函数。但是,即使在路径中写入了新值,我也感觉不到函数被触发运行。函数的日志保持为空 因为我不能在这里添加图像,所以我只能尽我所能来描述它。在数据库中,我们创建了通知。在通知中,当有人试图添加朋友时,它会将接收请求的人的用户id填充到通知中。然后,在该用户id中添加一个随机生成的通知id,其中该部分包含发送请求的用户和通知类型。下面是一个小图表Javascript Firebase函数日志为空,javascript,firebase,google-cloud-functions,Javascript,Firebase,Google Cloud Functions,因此,当一个值被添加到数据库中的通知时,我试图从firebase运行以下函数。但是,即使在路径中写入了新值,我也感觉不到函数被触发运行。函数的日志保持为空 因为我不能在这里添加图像,所以我只能尽我所能来描述它。在数据库中,我们创建了通知。在通知中,当有人试图添加朋友时,它会将接收请求的人的用户id填充到通知中。然后,在该用户id中添加一个随机生成的通知id,其中该部分包含发送请求的用户和通知类型。下面是一个小图表 通知 --->用户id ------>通知单 --------->从 -----
- 通知
- --->用户id
- ------>通知单
- --------->从
- --------->类型
“严格使用”
const functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification=functions.database.ref('/Notifications/{user\u id}/{notification\u id}')。onWrite(事件=>{
const user_id=event.params.user_id;
const notification_id=event.params.notification_id;
log('我们有一个通知要发送到:',用户id);
如果(!event.data.val()){
return console.log('已从数据库中删除通知:',通知id);
}
const deviceToken=admin.database().ref(`/Users/${user\u id}/device\u token`)。一次('value');
返回deviceToken.then(结果=>{
const token_id=result.val();
常数有效载荷={
通知:{
标题:“好友请求”,
正文:“你收到了一个新的朋友请求”,
图标:“默认”,
}
};
返回admin.messaging(){
log(“这是通知功能”);
});
});
});代码>
需要启用FCM消息发送API:
搜索FCM并在您的项目上启用感谢您的回复。我已经尝试了您发送的代码并启用FCM,但是仍然没有日志。我可以在数据库中看到如何编写新通知。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// There is a new signature for onWrite
exports.sendNotification =
functions.database.ref('/Notifications/{user_id}/{notification_id}')
.onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
// Exit when the data is deleted.
if (!change.after.exists()) {
console.log('A notification has been deleted from the database : ', notification_id);
return null;
}
console.log('We have a notification to send to : ', user_id);
return admin.database().ref(`/Users/${user_id}/device_token`).once('value')
.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Friend Request",
body: "You-ve recieved a new Friend Request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload);
})
.then(response => {
console.log('This was the notification feature');
return null;
})
.catch(err => {
console.log(err);
});
});